Jars

F

freesoft_2000

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
 
D

delasmooth

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
 
R

Remi Koutcherawy

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
 
R

Raymond DeCampo

freesoft_2000 said:
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
 
F

freesoft_2000

Hi everyone,

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

Thank You

Richard West
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top