How to run a jar file using java Runtime class

Hi,

How to run a jar file using Runtime class in java. Please suggest
CIPL-SenthilAsked:
Who is Participating?
 
Kevin CrossConnect With a Mentor Chief 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:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CEHJCommented:
Why not just do


OtherClass.main(null);

?
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.