0

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.

4
  • 1
    You probably need another thread to consume the program's output streams, otherwise this code dead-locks. Commented Jun 18, 2012 at 9:45
  • Can u make it clear, how to do that? Commented Jun 18, 2012 at 9:47
  • 1
    javaworld.com/jw-12-2000/jw-1229-traps.html Commented Jun 18, 2012 at 9:48
  • great link...it worked fyn..thanks a lot Commented Jun 18, 2012 at 12:59

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

actually jar is running,but it is getting stuck in middle
I suppose you can print the state/exceptions on the console and read the console using processbuilder outputs ? What does that show ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.