ProcessBuilder: Start .jar in java package

192 views Asked by At

I try to execute an external jar in my java application. The .jar is in my java-package ("gui"). I tried:

            String filepath = this.getClass().getResource("ServerRSS.jar").getPath();
            ProcessBuilder pb = new ProcessBuilder("java", filepath);

System.out.println(filepath); results in:

/C:/Users/hox/workspace/PraktikumProg/bin/gui/ServerRSS.jar

My programm doesn't start. Could the problem be the slash before the C: ?

EDIT: The solution was:

URL filepath = this.getClass().getResource("ServerRSS.jar");
ProcessBuilder pb;
pb = new ProcessBuilder("java", "-jar", new File(filepath.toURI()).toString());
Process p = pb.start();
1

There are 1 answers

0
GhostCat On BEST ANSWER

First get your command working ... simply on the command line.

And only then try to run it from within Java using a ProcessBuilder.

Simple answer is probably: to use -jar when invoking java.

java someJar.jar

does not work!

And yes, that slash matters big time. You simply want a fully correct file path there.

Finally: are you really sure you want to start a new JVM in order to run a main method in some class? You see, you could do that within your current JVM - without the additional performance and complexity cost of using a second JVM!