-
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
Code:
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
-
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.
-
Hi everyone,
Yes it helped
Thank You
Richard West
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks