Raj
Raj

Reputation: 440

error in running a jar using java code

I have a java swing application to extract text from pdf. It works fine when i run using command prompt(java -jar xyz.jar) or double click and run the jar but it gets stuck when I run using java code

Process asm = Runtime.getRuntime().exec("java -jar xyz.jar");
asm.waitFor();

or using process builder. is it because of the exceptions in my application? I'm not sure.

Upvotes: 0

Views: 213

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382122

You probably aren't in the proper directory.

Use the directory() method of ProcessBuilder so that the external process is run in the correct place so that the jar can be found.

ProcessBuilder processBuilder = new ProcessBuilder("java -jar xyz.jar");
processBuilder.redirectErrorStream(true);
processBuilder.directory(  complete this );
Process process = processBuilder.start();
String output = readOutput(process);
try {
    if (process.waitFor() != 0) {
        throw new IOException("command exited in error: " + process.exitValue() + "\n" + output);
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

If this doesn't solve your problem, print the output streams (including the error one) to see what happens.

Upvotes: 2

Related Questions