how to run a jar file by a java code (within ur IDE)?

Recommended Answers

why would you run a jar within an ide?
you should run your classes from your ide.

Jump to Post

In that case you can use the ProcessBuilder class - it lets you start an external process, set its working directory etc.
See the usual API doc, or Google for tutorials

Jump to Post

All 6 Replies

why would you run a jar within an ide?
you should run your classes from your ide.

Do you simply want to start the application in that jar executing, or do you want to use the classes inside that jar?

i simply want to use action listener to open a jar program within a certain directory

In that case you can use the ProcessBuilder class - it lets you start an external process, set its working directory etc.
See the usual API doc, or Google for tutorials

hmm, simplest way to launch a Java program from another Java program is to load the classes onto the active classpath, load the main class into a classloader, and then execute the main() method on that class.

No need to use ProcessBuilder to create a new JVM, just make sure you have the jar on your classpath and call SomeClass.main(new String[]) or something like that :)

SomeClass.main(null) or something like that...

Yes. That was the other solution I had in mind too, especially if he wants to interact with the classes in the jar directly.
ProcessBuilder would be more appropriate if the jar is just a "black box" application that happens to be in java, and especially if you want to pipe its sysin or sysout.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.