wood burning stoves 2.0*
The moose likes Java in General and the fly likes Running a JAR file from a java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running a JAR file from a java program" Watch "Running a JAR file from a java program" New topic
Author

Running a JAR file from a java program

igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
Hello, could anyone help me with this. I have a java program and I want to be able to execute a JAR file from within that program. Using Runtime.exec I can easily execute .exe files, how can I do the same with my jar files. Thank you.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

With Runtime.exec("java -jar myjar.jar") ?

Or you could use a java.net.URLClassLoader to load the main class, and execute it directly.


[Jess in Action][AskingGoodQuestions]
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Hey ... what on earth are you thinking of. Put the jar in the classpath and all the methods in them can be accessed.

So call the main program of the jar in your java code. That it !!!

Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Putting the jar file in the classpath is not going to help if you don't have the jar file in advance. I assume you want to do this without restarting your program.

So use a URLClassLoader to reference the jar file. Find out the main class from the Manifest. This will tell you the entry point. Use the URLClassLoader to load the main class, then invoke its main method with some arguments.



You will want to add exception handling to this example.

Geoffrey
[ May 10, 2005: Message edited by: Geoffrey Falk ]

Sun Certified Programmer for the Java 2 Platform
 
Consider Paul's rocket mass heater.
 
subject: Running a JAR file from a java program