0

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
3
  • @MadProgrammer It would not work as intended, since it would try to find sl class to run main function, which do not exist. Commented Sep 14, 2017 at 2:37
  • 1
    Have you tried flipping them? processBuilder.command("java","-cp","es.jar", "Main", arg).start(); - But I'm left thinking, can you send parameters to a class this way... Commented Sep 14, 2017 at 2:39
  • @MadProgrammer Flipping them does work! Thanks, that is the answer. Commented Sep 14, 2017 at 2:42

2 Answers 2

2

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();

Sign up to request clarification or add additional context in comments.

Comments

0

Please use the following command instead:

java -cp es.jar Main

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.