DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Jars

  1. #1
    Join Date
    Oct 2004

    Question Jars

    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 ()
    			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)
    	public static void main(String args[])
    		RunningApplications a = new RunningApplications(); 
    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

  2. #2
    Join Date
    Jul 2005
    well, as far as I can see, you have it right... Just remember that Runtime.exec() works just like a dos command in that you send the same text to it as you would in dos. So in dos, if you want to execute a .jar file, all you have to do is make sure that java.exe is in your PATH, or you refer to it directly (which it seems that you are doing), and then input the program name w/ the .jar extension (i.e. "JButtons.jar", again, you are already doing this).

    Now, your question was how to set the classpath. I don't think that you would set a classpath while executing a jar, instead what would happen is that it would be sent to the main(String[] args) method for processing. If, on the other hand, you are trying to compile a java file, you will need to set the classpath, and if you want more information on how to do that, please refer to my post here.

    If you would like me to explain something further, just ask. Hope this helped.

  3. #3
    Join Date
    Oct 2004
    Hi everyone,

    Yes it helped

    Thank You

    Richard West

  4. #4
    Join Date
    Jul 2005
    SW MO, USA
    I don't think the commandline you are executing is correct. The -cp should not be there.
    It should be: java.exe -jar name.jar

Similar Threads

  1. executable jars
    By goodnick in forum Java
    Replies: 1
    Last Post: 03-23-2001, 03:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center