Nin
Nin

Reputation: 407

Java ProcessBuilder calling another java with arguments

I want to use ProgressBuilder to run another jar with arguments inserted.

Example: es.jar file with Main class, and an arg sl. I know I can use Runtime.getRuntime().exec() like this:

String arg = "sl";
Process p = Runtime.getRuntime().exec("java -cp \"es.jar " + arg + "\" Main");

If I use ProgressBuilder, without the arg sl, it would be like this:

processBuilder.command("java","-cp","es.jar", "Main").start();

However, arg sl is needed, how can I insert it? I have tried the following codes but obviously none of them work:

processBuilder.command("java","-cp","es.jar sl", "Main").start(); // failed
processBuilder.command("java","-cp","\"es.jar sl\"", "Main").start(); // failed also

Upvotes: 0

Views: 1837

Answers (2)

user1494732
user1494732

Reputation:

Please use the following command instead:

java -cp es.jar Main

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

java -cp "es.jar sl" Main makes no sense, and I'm pretty sure it would otherwise fail

java states java [-options] class [args...], so the first parameter after the options is the class to be executed, so based on your question, that would mean it needs to be

java -cp es.jar Main sl, or in more specifically, in regards to your question, processBuilder.command("java","-cp","es.jar", "Main", arg).start();

Upvotes: 2

Related Questions