rilent
rilent

Reputation: 722

ProcessBuilder run jar on Linux

I am trying to run a jar on a Linux server with this code :

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;
}

And I get this error :

java.io.IOException: Cannot run program "/usr1/linktojava/java/jdk/java" (in directory "/usr1/oracle/directory"): error=2, File or directory not found

All files seem to be at the correct place.

Upvotes: 0

Views: 458

Answers (1)

Thomas Fritsch
Thomas Fritsch

Reputation: 10127

The java executable is located within the jre/bin/ sub-directory of the JDK.

Hence, the correct path to be passed to the ProcessBuilder would be "/usr1/linktojava/java/jdk/jre/bin/java" instead of "/usr1/linktojava/java/jdk/java".

Upvotes: 1

Related Questions