"Runtime.getRuntime (). Exec ()" has poor performance?

I want to execute a jar from my own Java application. (it is impossible to import this jar into the library and run the application as an instance of my own β€œlauncher”). To execute a jar from my own java application ... I use the following lines:

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

It works great. I do not complain about it.

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

+4
source share
3 answers

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

  • The current process is forked.
  • The new child process executes the java command, passing the specified command line arguments.
  • starting child JVM ...

There may be secondary differences in performance. For example, in the way that child standard input / output / error streams are processed by the parent, can be different. But usually you can forget about it.

[As @Amadan points out, using the class loader to run a Java application in the current JVM is much more efficient ... because it avoids the overhead of running a JVM, compiling JIT common code, etc. But the main minus (except for simplicity) is that there is no effective way for a "parent" application to manage a "child" application that runs in one JVM. If a child gets stuck in a cycle or is messy with resource management at all, the parent also suffers.]

+3
source

Same.

Executing a process executes a process, regardless of whether it executes a shell or your application.

+1
source

In any case, this is one and the same. Use the new api ProcessBuilder, which has a better way of specifying arguments.

+1
source

All Articles