开发者

Exec Command doesnt seem to be executing properly in java

I have a java project built that I want to run from another java program. But when I call it using the exec command it does not execute immediately but waits for the curre开发者_如何转开发nt program to end. If I add a waitFor statement then the Program hangs as the main program waits for the process and the Process is waiting for the Main program. Does anyone know how I can solve this? Or why it is behaving in such a manner? I need this jar file to execute before a second one can.

Runtime.getRuntime().exec("java -jar \"JavaProject1/dist/JavaProject1.jar\"");
        System.out.println("Hello");
        p.waitFor();

The location of the jar file is fine and it prints the hello. The main class for the jar file I want to run is in this thread at DaniWeb


Amongst other things, you have to keep reading from the processes STDOUT and STDERR, otherwise it will block.

See, for example, http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html


why it is behaving in such a manner?

Because You are creating deadlock in your own program.

When you add waitFor() your program main() in this case waits for the external process to get finished.

It returns Zero when the external process gets fnished normally.

May be the external program has some issues

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜