Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Running a JAR file from a java program  RSS feed

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Runtime.exec("java -jar myjar.jar") ?

Or you could use a java.net.URLClassLoader to load the main class, and execute it directly.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!

 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!