ProcessBuilder在Linux上运行jar

时间:2018-11-27 10:40:06

标签: java linux jar processbuilder

我正在尝试使用以下代码在Linux服务器上运行jar:

private static final String directory = "/usr1/oracle/directory/";

protected ProcessBuilder buildImportProcess(String[] args) {
    ProcessBuilder pb = new ProcessBuilder("/usr1/linktojava/java/jdk/java", "-jar", directory + "jartoexecute.jar",
            args[0], args[1], args[2]);
    pb.directory(new File(directory));
    return pb;
}

public int runJar(String[] args) {
    int status = 1;
    try {
        ProcessBuilder pb = buildImportProcess(args);
        Process process = pb.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String s = "";
        while ((s = in.readLine()) != null) {
            logger.info(s);
        }
        status = process.waitFor();
    } catch (IOException | InterruptedException e) {
        logger.error(e.getMessage(), e);
    }
    return status;
}

我收到此错误:

  

java.io.IOException:无法运行程序   “ / usr1 / linktojava / java / jdk / java”(在目录中   “ / usr1 / oracle / directory”):error = 2,找不到文件或目录

所有文件似乎都放在正确的位置。

1 个答案:

答案 0 :(得分:1)

java可执行文件位于JDK的jre/bin/子目录中。

因此,要传递到ProcessBuilder的正确路径 将是"/usr1/linktojava/java/jdk/jre/bin/java" 而不是"/usr1/linktojava/java/jdk/java"