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 / January 2007

Tip: Looking for answers? Try searching our database.

How do I launch a new Java app from a running Java app?

Thread view: 
steve.albin@gmail.com - 28 Dec 2006 18:57 GMT
Here is the motivation for my question. I have a Swing-based
application that is launched using Web Start. I would like this
application to launch other application instances using it's own
classpath (e.g. fork new instances of itself) when the user invokes
certain functions.

I know that I can use Runtime or ProcessBuilder to launch another Java
application. If I use this approach, how can I get the current
application's classpath so that I can pass it as an argument to the
Java sub process?

Sincerely,
Steve A
steve - 03 Jan 2007 08:56 GMT
> Here is the motivation for my question. I have a Swing-based
> application that is launched using Web Start. I would like this
[quoted text clipped - 9 lines]
> Sincerely,
> Steve A

The following  segment works on windows/mac & linux and has been in use for
over 2 years in our office

Fairly easily, basically I use it for deploying updates.
I have a class called "bootloader" that contains the following,  the most
important thin is that  your "bootloader" class MUST NOT link into any of
your other libraries. (if it does then the JVM goes on a class hunt trying to
resolve all other classes & preload them)

That allows you to use the bootloader to auto update any other files , other
than "bootloader"

OurJarClassPathArray, holds that paths of any jars you may need (libraries,
DO NOT HARDCODE, or use  imports)

loadThisClass , holds the class you want to run

 final Method mainMethod;
       final Class toRun;

 ClassLoader loader = new URLClassLoader(OurJarClassPathArray);
      toRun = loader.loadClass(loadThisClass);
     mainMethod = findMain(toRun, thisargs);

    if ((mainMethod != null)) {
               //launch the new program in a new thread so we can exit the
bootloader (can we really?)
               mainThread =
                   new Runnable() {
                           public void run() {
                               try {
                                   final String[] dummy = null;
                                   mainMethod.invoke(null,
                                       new Object[] { dummy });
                               } catch (Exception e) {
                                   System.out.println(
                                       "Exception Caused when trying to
Execute main Class");
                                   e.printStackTrace();
                               }
                           }
                       };
                       //this appears to be important code that was stopping
the sh.t
                       //running on the linux correctly, specifically the
classloader.
               Thread mainRunner=new Thread(mainThread);
             mainRunner.setContextClassLoader(loader );
              mainRunner.start();
System.out.println("We think have exited the BootLoader ");
       System.gc();
Andrew Thompson - 03 Jan 2007 10:04 GMT
> Here is the motivation for my question. I have a Swing-based
> application that is launched using Web Start. I would like this
> application to launch other application instances using it's own
> classpath (e.g. fork new instances of itself) when the user invokes
> certain functions.

Why not instantiate the new instance directly
from the code in the original instance?  E.G.

class Application {
...
  public void actionPerformed(ActionEvent ae) {
    if (...) {
      Application app = new Application();
   } else if ....

Andrew T.
Daniel Pitts - 03 Jan 2007 16:59 GMT
> > Here is the motivation for my question. I have a Swing-based
> > application that is launched using Web Start. I would like this
[quoted text clipped - 13 lines]
>
> Andrew T.

Or even better...

public void actionPerformed(ActionEvent ae) {
  if (shouldSpawnNewApp) {
     Thread thread = new Thread() {
          public void run() {
              MyMainClass.main(new String[] {} );
          }
     };
     thread.setPriority(Thread.NORM_PRIORITY);
     thread.start();
  }
}


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.