wood burning stoves 2.0*
The moose likes Java in General and the fly likes run jar from java application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "run jar from java application" Watch "run jar from java application" New topic
Author

run jar from java application

terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Sorry if this should have been put in one of the categories, couldn't find one that would fit..

I have my main application, let's call it mainApp. I'm making a small application that's checks for updates for mainApp, downloads file and replaces old file "mainApp.jar". What i can't figure out is how i can run the file "mainApp.jar" from inside "updateApp".

I tried this in many syntaxes:
Process proc = Runtime.getRuntime().exec("cmd /C java -jar 'C://Program Files//Frontlinesystems/mainApp.jar' ");

i suspect by running this the "result" from mainApp.jar somehow is stored in the proc variable?

how should i do this?

tnx
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

All else aside, why do you have double forward slashes in the file path?


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Read When Runtime.exec() won't. Using ProcessBuilder and let it redirect the error stream to the output stream can make life a little bit easier for you.

Now that you're reading the output from the process, you can get the information you need from it.


Another, completely different option, is to not use Runtime.exec / ProcessBuilder but call the main method from Java.
1) the JAR file is part of the class path, both at compile time and at runtime. Just call the static main method:

2) the JAR file isn't part of the class path:

Of course it's possible you don't know the main class from the JAR file. Using java.util.jar.JarFile and java.util.jar.Manifest you can retrieve it:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Thanks Rob Spoor!

I have never thought about why it needs to be double slashes, but that's just the way it is

I will try this out. I also found another solution involving running a bat file from the updater that starts the main application. Would you say one method is better than the other?

Stian
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I didn't even notice I copied the double slashes. You don't need those; you would need double backslashes (\\) because backslash is the escape character in Strings.

Using a separate process (either through cmd /c or through a bat file) has the advantage that any System.exit calls in the Java code will not exit your current application. The drawback is that you will need to take care of the issues described in the article I mentioned.
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Ok, thanks for your help!

Stian
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: run jar from java application
 
Similar Threads
Jar manifest problem
JMS Question
Problem in Eclipse 3.4 running Java program
Problem in executing java project jar file
Sample Project