1

I want to run a jar script from within a Java program. The command is tested to run without problems if pasted sloley to the Windows cmd. However, my Java script stucks at p.waitFor().

public class MyClass{

   public static void main(String[] args) {

     try {
       // create a new process
       System.out.println("Creating Process...");
       Process p = Runtime.getRuntime().exec("U:\\locallib\\jdk-8u65-windows-x64\\tools\\bin\\java.exe -Xmx4g -jar U:\\path\\to\\my.jar arg1 arg2");
       System.out.println("Process is running...");

       p.waitFor();
       System.out.println("Process is terminated...");
     } 
     catch (Exception ex) {
       ex.printStackTrace();
     }
   }
}

Ouput:

Creating Process...
Process is running...

...and nothing else.

7
  • Can you see it still running in the task manager? Does it wait for input or anything like that? Commented Apr 22, 2016 at 11:05
  • It's listed, but seems to sleep. ...and it doesn't require input if running sloley in the Windows cmd. Commented Apr 22, 2016 at 11:12
  • i think you dont have a problem with the code Commented Apr 22, 2016 at 11:12
  • The jar script writes to STDOUT. Might that cause the problem? Commented Apr 22, 2016 at 11:13
  • 2
    You generally need to read the Process getInputStream (and also the getErrorStream), if you don't the program may become blocked when the buffers become full. Commented Apr 22, 2016 at 11:32

0

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.