开发者

Does "Runtime.getRuntime().exec()" have a bad performance?

I want to execute a jar from my own java application. (it's impossible to import that jar a library and start the app as an instance of my own "launcher"). To execute a jar from my own java app...I am using the next lines:

开发者_开发技巧
String [] cmd = new String [] {"java","-jar","myjar.jar"};
Process process = Runtime.getRuntime().exec(cmd, null, null);

This perfectly works. I have no complains about this.

My question is this: does this have the same performance as executing that jar by "java -jar myjar.jar" in the command line? Or is it worse?? If worse...any suggestion about can I do this with the same performance?


The performance is essentially the same, because the essentially the same thing is happening in both cases. For example, on a UNIX / Linux platform:

  • The current process is 'forked'.
  • The new child process 'exec's the 'java' command, passing the specified command line arguments.
  • The child JVM starts ...

There might be secondary performance differences. For example in the way that the child's standard input / output / error streams get handled by the parent may be different. But usually you can forget about this kind of thing.

[As @Amadan notes, using a class loader to launch the Java application in the current JVM is considerably more efficient ... because it avoids the overheads of JVM startup, JIT compilation of common code, etc. But the main downside (apart from simplicity) is that there's no effective way for the "parent" application to control a "child" application that is running in the same JVM. If the child gets stuck in a loop or is generally sloppy with resource management, the parent suffers too.]


It's the same.

Executing a process is executing a process, whether the command processor does it or your application.


By all means it is same. Use new api ProcessBuilder, which has better way of specifying arguments..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜