How to launch One Java Program from another Java Program

Discussion in 'Java' started by joshivaibhav, Nov 17, 2006.

  1. joshivaibhav

    joshivaibhav Guest

    Hello,

    I am new to Java and here is my question:

    How to launch another Java program from within a Java program?

    I have tried using Runtime.getRuntime() and it works for launching
    applications.

    But if I need to launch another java program do I have to use:

    "Java <another Java program name>" => This string as an input to exec()
    method to run another Java program?

    Both the programs will run on the same machine. Can these program share
    the same JVM and run in the same process?

    I want to avoid using RMI.

    Please advice.

    Thanks,

    Vaibhav
     
    joshivaibhav, Nov 17, 2006
    #1
    1. Advertising

  2. joshivaibhav

    Jeff Guest

    joshivaibhav wrote:
    > Hello,
    >
    > I am new to Java and here is my question:
    >
    > How to launch another Java program from within a Java program?
    >
    > I have tried using Runtime.getRuntime() and it works for launching
    > applications.
    >
    > But if I need to launch another java program do I have to use:
    >
    > "Java <another Java program name>" => This string as an input to exec()
    > method to run another Java program?
    >
    > Both the programs will run on the same machine. Can these program share
    > the same JVM and run in the same process?
    >
    > I want to avoid using RMI.
    >
    > Please advice.
    >
    > Thanks,
    >
    > Vaibhav


    It seems to me that you would just use a classLoader to load the class
    and execute the main method.
     
    Jeff, Nov 17, 2006
    #2
    1. Advertising

  3. joshivaibhav wrote:
    >
    > Both the programs will run on the same machine. Can these program share
    > the same JVM and run in the same process?


    You can, but they wont be entirely independent.

    o Create a new ClassLoader with the URLs of the program code. null as
    the parent class loader will stop the libraries you are using interfere
    with the libraries it is using.

    ClassLoader loader = URLClassLoader.newInstance(new URL[] { url },
    null);

    http://download.java.net/jdk6/docs/api/java/net/URLClassLoader.html#newInstance(java.net.URL[],
    java.lang.ClassLoader)

    o Find you the applications main class (I guess you could look up
    Main-Class in its manifest).

    Class<?> mainClass = Class.forName(mainName, false, loader);

    http://download.java.net/jdk6/docs/api/java/lang/Class.html#forName(java.lang.String,
    boolean, java.lang.ClassLoader)

    o Find the main method.

    Method mainMethod = mainClass.getMethod("main", String[].class);

    http://download.java.net/jdk6/docs/api/java/lang/Class.html#getMethod(java.lang.String,
    java.lang.Class...)

    o Invoke main method. Note how varargs doesn't work too well.

    mainMethod.invoke(null, new Object[] { new String[] { /* args */ }});

    http://download.java.net/jdk6/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object,
    java.lang.Object...)

    (Disclaimer: I've not tested or even compiled this code. However, I have
    written similar stuff before).

    You will be sharing the JVM, but for AWT/Swing applications you wont be
    able to do the separate AppContext thing. You can create a new
    ThreadGroup for the application if you so wish.

    > I want to avoid using RMI.


    It's not *that* bad is it?

    Tom Hawtin
     
    Thomas Hawtin, Nov 18, 2006
    #3
  4. joshivaibhav

    joshivaibhav Guest

    Thanks !!!

    I will try this out and see if it solves my problem.

    Thomas Hawtin wrote:
    > joshivaibhav wrote:
    > >
    > > Both the programs will run on the same machine. Can these program share
    > > the same JVM and run in the same process?

    >
    > You can, but they wont be entirely independent.
    >
    > o Create a new ClassLoader with the URLs of the program code. null as
    > the parent class loader will stop the libraries you are using interfere
    > with the libraries it is using.
    >
    > ClassLoader loader = URLClassLoader.newInstance(new URL[] { url },
    > null);
    >
    > http://download.java.net/jdk6/docs/api/java/net/URLClassLoader.html#newInstance(java.net.URL[],
    > java.lang.ClassLoader)
    >
    > o Find you the applications main class (I guess you could look up
    > Main-Class in its manifest).
    >
    > Class<?> mainClass = Class.forName(mainName, false, loader);
    >
    > http://download.java.net/jdk6/docs/api/java/lang/Class.html#forName(java.lang.String,
    > boolean, java.lang.ClassLoader)
    >
    > o Find the main method.
    >
    > Method mainMethod = mainClass.getMethod("main", String[].class);
    >
    > http://download.java.net/jdk6/docs/api/java/lang/Class.html#getMethod(java.lang.String,
    > java.lang.Class...)
    >
    > o Invoke main method. Note how varargs doesn't work too well.
    >
    > mainMethod.invoke(null, new Object[] { new String[] { /* args */ }});
    >
    > http://download.java.net/jdk6/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object,
    > java.lang.Object...)
    >
    > (Disclaimer: I've not tested or even compiled this code. However, I have
    > written similar stuff before).
    >
    > You will be sharing the JVM, but for AWT/Swing applications you wont be
    > able to do the separate AppContext thing. You can create a new
    > ThreadGroup for the application if you so wish.
    >
    > > I want to avoid using RMI.

    >
    > It's not *that* bad is it?
    >
    > Tom Hawtin
     
    joshivaibhav, Nov 21, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lucas
    Replies:
    7
    Views:
    811
    Thomas Hawtin
    Sep 14, 2005
  2. loveNUNO
    Replies:
    2
    Views:
    933
    loveNUNO
    Nov 20, 2003
  3. KDawg44

    Launch another application

    KDawg44, Apr 23, 2007, in forum: Python
    Replies:
    1
    Views:
    311
    Larry Bates
    Apr 23, 2007
  4. Yao Qi
    Replies:
    30
    Views:
    1,377
    Roedy Green
    Jul 4, 2007
  5. Robert Cohen
    Replies:
    3
    Views:
    283
    Andrew Durstewitz
    Jul 15, 2003
Loading...

Share This Page