views:

91

answers:

3

Hi there,

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???

Thanks in advanced

+1  A: 

It's the same.

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

EJP
could you give me a brief explanation or reference? Thanks!
testk
I confirm, it is the *same*. Which is less efficient but much easier than going through a `ClassLoader`.
Amadan
+1  A: 

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.]

Stephen C
A: 

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

Jayan