• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

How to run a jar file using java Runtime class

Hi,

How to run a jar file using Runtime class in java. Please suggest
0
CIPL-Senthil
Asked:
CIPL-Senthil
  • 3
  • 3
  • 3
  • +1
1 Solution
 
CEHJCommented:
You can only do that by invoking an app with a main method. Why don't you do that directly from the other app instead of using Runtime?
0
 
CIPL-SenthilAuthor Commented:
sorry i didn't understand
0
 
CEHJCommented:
Why not just do


OtherClass.main(null);

?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kevin CrossChief Technology OfficerCommented:
What CEHJ is saying is why not do like this:
import other.jar.package.JarMainClassName;
 
class MyClassName {
    public static void main(String[] args) {
        // invoke jar class...
        JarMainClassName.main(args);
    }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Ah, there is the legend himself.  :)
0
 
CIPL-SenthilAuthor Commented:
I think by using runtime we can do that. I want to know how to do that using runtime only.
please suggest
0
 
Kevin CrossChief Technology OfficerCommented:
Are you really looking to do this:

 try {
            Runtime.getRuntime().exec("java -jar MyJar.jar");
        } catch (IOException ex) {
            // trace error message
        }

Have a look here, maybe this is what you want:
http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime
0
 
CEHJCommented:
>>I think by using runtime we can do that. I want to know how to do that using runtime only.

In the way mwvisa1 showed
0
 
objectsCommented:
when using exec() you need to ensure you consume any output from the process.
Theres a useful utility class at the following link
http://helpdesk.objects.com.au/java/runtime-exec-locks-up
Also read the link in the comments which discusses vaious gotchas when using exec()

0
 
CIPL-SenthilAuthor Commented:
Thank you for all the suggestions. I got the solution. we have to give in this following manner
rt.exec(cmd /c start java -jar Myjar.jar).
0
 
objectsCommented:
yes the link I posted above explains that, you need to give it a shell

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now