从Java类中运行Tomcat服务器 - ProcessBuilder

时间:2016-10-31 10:40:40

标签: java tomcat automation processbuilder

我在尝试使用Java运行进程时遇到了一个问题。特别是尝试从我的Java应用程序中运行tomcat服务器。我花了几个小时研究stackoverflow和google上的所有帖子,尝试了不同的解决方案,对我来说没有任何作用。基本上,我正在构建一个自动化测试工具,允许测试在不同的站点上运行。我已经对Process和ProcessBuilder进行了treid来为我执行这个任务。尝试和调用tomcat服务器有三种方法,如下所示:

public void convertCouncilNameToExecutableProcess(String councilName) {

    System.out.println("In convert method");
    if (councilName.equalsIgnoreCase("Manchester")) {
        councilRunner = "claims_manchestertest";
        runCouncilProcess(councilRunner);
    } else if (councilName.equalsIgnoreCase("Coventry")) {
        councilRunner = "claims_coventrytest";
        runCouncilProcess(councilRunner);
    }

}


private void runCouncilProcess(String councilName) {

    try {
        List<String> commandList = buildCommandList(councilName);
        ProcessBuilder buildProcess = new ProcessBuilder(commandList);
        buildProcess.start();
        String output = IOUtils.toString(buildProcess.start().getInputStream());
        System.out.println("In process method " + output);
        System.out.println("Council: " + councilName + " is running successfully..");
    } catch (IOException e) {
        e.printStackTrace();
    }
}


private List<String> buildCommandList(String councilName) {
    List<String> command = new ArrayList<String>();
    command.add("sudo");
    command.add("/opt");
    command.add("/" + councilName);
    command.add("/tomcat");
    command.add("/bin");
    command.add("/becs_tomcat");
    command.add(" console");
    return command;

}

由于我们有多个理事会可以使用,代码需要根据要测试的站点区分应运行哪个tomcat ...

代码运行时的输出如下:

manchester
In convert method
In process method 
Council: claims_manchestertest is running successfully..

每个方法都会执行,告诉我代码被命中但运行不正确......

然后我检查我的终端以查看是否已使用以下命令初始化tomcat服务器:

ps -ax tomcat

但是,没有运行tomcat实例!?

请帮忙!如果您需要更多信息,请问我。感谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我找到了一个解决方案......运行shell脚本似乎对我有用。只需将路径传递给shell脚本的ProcessBuilder和参数即可。然后tomcat服务器将成功启动。继承了我的解决方案:

    String runShellScriptPath = "/Users/joshuadunn/workspace/update/startTomcat.sh";



private void runCouncilProcess(String councilName) {
    try {
        ProcessBuilder buildProcess = new ProcessBuilder(runShellScriptPath, councilName);

 **** Specifically the line above - Takes the string path and the council name for the correct tomcat to be started ****

        buildProcess.start();

        System.out.println("Council: " + councilName + " is running successfully..");
    } catch (IOException e) {
        e.printStackTrace();
    }
}