1

I am able to invoke a java class using below command line:

java -jar <my jar name>.jar -option1 <option> "<a string value>"

However, I want to invoke it from a another Java program. The name of the main class is XYZ

I have imported the jar as a dependency in my project.

1
  • 3
    Do you want to launch it as a separate process, or just in-process? What happens if you just call the main method with the relevant arguments? Commented Feb 18, 2016 at 8:27

1 Answer 1

1

That heavily depends on whether you want to launch it as a separate process or simply execute within the same one.

To kick it off in the same process, you could just call the main method:

String[] arguments = new String[]{"-option1", "<option>", "<a string value>"};
MyOtherMainClass.main(arguments);

To start it as a different process:

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "<my jar name>.jar", "-option1", "<option>", "<a string value>");
Process process = pb.start();
int errCode = process.waitFor();

The second example was inspired by http://examples.javacodegeeks.com/core-java/lang/processbuilder/java-lang-processbuilder-example/

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.