Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

how to run a jar, from another jar.

Thread view: 
tiewknvc9 - 30 Sep 2006 07:02 GMT
Hi.

As the subject suggests. I am trying to run an application bundled in a
jar1 file, the from that jar1 file, I would like to run the jar2 file
and close jar1.

The idea is that jar1 is an updater and will check online and install
updates to jar2, then jar2 (the main app) will start up, close jar1,
and update jar1 (if neccessary).  The thing that Im struggling with is
how to actually run the jar2 file.

Thanks for your help!
hiwa - 30 Sep 2006 10:30 GMT
> Hi.
>
[quoted text clipped - 8 lines]
>
> Thanks for your help!
What's the problem you pesume?
Ben_ - 30 Sep 2006 12:36 GMT
Open jar2's manifest file, look at the main class defined and invoke its
main method using the reflection API (see java.lang.Class and
java.lang.reflect.Method).
Jeff - 30 Sep 2006 17:29 GMT
Check out URLClassLoader (or ClassLoader, and pick which one works best
for your application) to create an object of the desired class, then
your java.lang.reflect,Method routines to execute the desired method.

> Open jar2's manifest file, look at the main class defined and invoke its
> main method using the reflection API (see java.lang.Class and
> java.lang.reflect.Method).
tiewknvc9 - 01 Oct 2006 16:48 GMT
> Check out URLClassLoader (or ClassLoader, and pick which one works best
> for your application) to create an object of the desired class, then
[quoted text clipped - 3 lines]
> > main method using the reflection API (see java.lang.Class and
> > java.lang.reflect.Method).

ok, I imported the 2 classes JarRunner and JarClassLoader/

and it works... to a degree.

It does seem to load the main class! (hooray!), and try to run the
application, however Im having a strange problem...  The 2nd jar, is
having trouble loading the images that are packaged inside the 2nd jar!

It reports a nullPointerException when it is trying to load an image...
Im using the standard

Image img=
getToolkit().getImage(ClassLoader.getSystemResource(m_strImage));

what am I doing wrong?
Jeff - 01 Oct 2006 20:44 GMT
> > Check out URLClassLoader (or ClassLoader, and pick which one works best
> > for your application) to create an object of the desired class, then
[quoted text clipped - 19 lines]
>
> what am I doing wrong?

There are issues with using ClassLoader, in that there are multiple
versions that will look at different locations for loading the class in
question. Best source to understand this is a white paper on
Class.forName - see
http://www.theserverside.com/tt/articles/content/dm_classForname/DynLoad.pdf
tiewknvc9 - 01 Oct 2006 21:42 GMT
> > > Check out URLClassLoader (or ClassLoader, and pick which one works best
> > > for your application) to create an object of the desired class, then
[quoted text clipped - 25 lines]
> Class.forName - see
> http://www.theserverside.com/tt/articles/content/dm_classForname/DynLoad.pdf

ok, now I am able to load the class!  Sweet!

But it still doesn't run, because it cannot find the images stored in
the very same jar file....

the jar file is on the classpath, and I do use the
ClassLoader.getSystemResource(strFile) to read the file from the main
function... so why isn't it finding the images?

If I run the jar by itself, it runs perfectly.  However when I run the
main class of that jar from another jar, it cannot find the images....
I hope its something simple to correct....  any ideas?
Jeff - 02 Oct 2006 00:08 GMT
> > > > Check out URLClassLoader (or ClassLoader, and pick which one works best
> > > > for your application) to create an object of the desired class, then
[quoted text clipped - 38 lines]
> main class of that jar from another jar, it cannot find the images....
> I hope its something simple to correct....  any ideas?

Try using URLClassLoader. With that you can specify exactly where the
loader should look for the class or info. You can test it to be sure it
finds it by using the findResource method.
tiewknvc9 - 02 Oct 2006 00:44 GMT
for those of you who have been searching for the answer..... do this!

cmd = "javaw -jar jar2.jar";

Process proc = Runtime.getRuntime().exec(cmd);

> > > > Check out URLClassLoader (or ClassLoader, and pick which one works best
> > > > for your application) to create an object of the desired class, then
[quoted text clipped - 38 lines]
> main class of that jar from another jar, it cannot find the images....
> I hope its something simple to correct....  any ideas?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.