Jars

Discussion in 'Java' started by freesoft_2000, Aug 1, 2005.

  1. Hi everyone,

    I have a rather newbie question so please bear with me
    for a while.

    You see i am trying to run a jar file programmatically
    and i am not quite sure how to do it. You see i use windows and i always
    double-click on it to run but now i have a requirement to run a jar file
    programmatically.

    This is what i have so far

    import java.awt.*;
    import java.awt.event.*;

    public class RunningApplications

    {


    public void runapplications ()
    {
    try
    {
    String str1 = "C:/j2sdk1.4.2_04/bin/java";
    String str2 = (".;" + "C:/");
    String str3 = "JButtons.jar";
    String[] str4 ={
    str1, "-jar","-cp", str2, str3
    }
    ;
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(str4);
    }

    catch(Exception e)
    {
    e.printStackTrace();
    }

    }

    public static void main(String args[])
    {
    RunningApplications a = new RunningApplications();
    a.runapplications();
    }
    }

    My java.exe is as located as shown by the value of str1 and my
    jar file is located at "C:\JButtons.jar". Basically all i need is a way to
    run that jar file and seeting the classpath programmatically by using the
    Runtime class.

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
     
    freesoft_2000, Aug 1, 2005
    #1
    1. Advertising

  2. freesoft_2000

    Guest

    The way you're trying to do it, it seems much easier to use dos scripts
    to get the job done. The more practical way to do it is using ant
    (http://ant.apache.org/). This enables you to define the classpath
    folders, and tasks in a simple xml document. Ant is very useful in
    doing batch processes. If you give me more specifics, I can recommend
    the best/easiest way to perform the task.

    Ed
     
    , Aug 1, 2005
    #2
    1. Advertising

  3. freesoft_2000 a écrit :
    > You see i am trying to run a jar file programmatically
    > and i am not quite sure how to do it.


    There are many ways :
    - Use reflection ie. >java mylaucher myjar.jar
    see http://www-sor.inria.fr/~dedieu/java/SplashScreen.java
    - Use Runtime.exec see
    http://cvs.sourceforge.net/viewcvs....rc/sf/eclipse/javacc/JarLauncher.java?rev=1.1
    - Use a ClassLoader see
    http://cvs.sourceforge.net/viewcvs..../src/sf/eclipse/javacc/JarLoader.java?rev=1.1

    These are complete working examples, you can first study them, then choose your way.

    Have fun.
    Rémi
     
    Remi Koutcherawy, Aug 1, 2005
    #3
  4. freesoft_2000 wrote:
    > Hi everyone,
    >
    > I have a rather newbie question so please bear with me
    > for a while.
    >
    > You see i am trying to run a jar file programmatically
    > and i am not quite sure how to do it. You see i use windows and i always
    > double-click on it to run but now i have a requirement to run a jar file
    > programmatically.
    >
    > This is what i have so far
    >
    > import java.awt.*;
    > import java.awt.event.*;
    >
    > public class RunningApplications
    >
    > {
    >
    >
    > public void runapplications ()
    > {
    > try
    > {
    > String str1 = "C:/j2sdk1.4.2_04/bin/java";
    > String str2 = (".;" + "C:/");
    > String str3 = "JButtons.jar";
    > String[] str4 ={
    > str1, "-jar","-cp", str2, str3
    > }
    > ;
    > Runtime rt = Runtime.getRuntime();
    > Process p = rt.exec(str4);
    > }
    >
    > catch(Exception e)
    > {
    > e.printStackTrace();
    > }
    >
    > }
    >
    > public static void main(String args[])
    > {
    > RunningApplications a = new RunningApplications();
    > a.runapplications();
    > }
    > }
    >
    > My java.exe is as located as shown by the value of str1 and my
    > jar file is located at "C:\JButtons.jar". Basically all i need is a way to
    > run that jar file and seeting the classpath programmatically by using the
    > Runtime class.
    >
    > Any help is greatly appreciated
    >


    It looks like your command translates to:

    C:/j2sdk1.4.2_04/bin/java -jar -cp .;C:/ JButtons.jar

    Which, as you discovered, is not correct. Read the documentation on the
    java.exe tool. It comes with the JDK/SDK. You will find that the
    classpath switches do not have any effect when used with -jar.

    The other issues you have include not immediately following -jar with
    the name of the jar and not specifying the full path of the jar. You
    want the following command:

    C:/j2sdk1.4.2_04/bin/java -jar C:/JButtons.jar

    HTH,
    Ray
    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Aug 1, 2005
    #4
  5. Hi everyone,

    Beautiful responses from all three of you guys. All your
    ways worked wonderfully

    Thank You

    Richard West
     
    freesoft_2000, Aug 2, 2005
    #5
    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. Wendy S

    Re: classpath & too many jars

    Wendy S, Jul 22, 2003, in forum: Java
    Replies:
    1
    Views:
    475
    Wendy S
    Jul 22, 2003
  2. Ike
    Replies:
    6
    Views:
    1,081
    Andrew Thompson
    Sep 24, 2004
  3. Replies:
    5
    Views:
    9,087
  4. JavaEnquirer
    Replies:
    2
    Views:
    537
    JavaEnquirer
    Feb 22, 2006
  5. Ulf Meinhardt
    Replies:
    2
    Views:
    1,422
Loading...

Share This Page