java.io.IOException:无法运行程序"。\ bin \ catalina.bat" - CreateProcess error = 2,系统找不到指定的文件

时间:2016-03-31 15:09:12

标签: java windows batch-file tomcat

尝试使用Java开始Tomcat进程时出现以下错误。

java.io.IOException: Cannot run program ".\bin\catalina.bat" (in directory "C:\Users\chiranga93\Documents\GitHub\product
-as\integration-tests\target\wso2as-6.0.0-SNAPSHOT"): CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at org.wso2.appserver.test.integration.TestSuiteListener.startPlatformDependApplicationServer(TestSuiteListener.
java:152)

用于此目的的代码段如下:

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.directory(appserverHome);
        processBuilder.environment().put("JAVA_OPTS", jacocoArg);

        if (os.toLowerCase().contains("windows")) {
            log.info("Starting server as a " + os + " process");
            return applicationServerProcess = processBuilder.command("\\bin\\catalina.bat", "run").start();
        } else {
            log.info("Starting server as a " + os + " process");
            return applicationServerProcess = processBuilder.command("./bin/catalina.sh", "run").start();
        }

bin文件夹中存在CATALINA_HOME\bin\catalina.bat文件。但它指定特定文件不存在。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

调用processBuilder.directory(File)仅为创建的进程设置工作目录,但不用于命令字符串本身。

因此,将命令设为绝对路径,例如

if (os.toLowerCase().contains("windows")) {
    String catalinaPath = new File(appserverHome, "bin\\catalina.bat").getAbsolutePath();
    return applicationServerProcess = processBuilder.command(catalinaPath, "run").start();
} ...