How to execute a jar file inside a swing application and wait for completion

Posted on 2009-04-27
Medium Priority
Last Modified: 2013-11-23
Hi Guys,
I am trying to use an existing application in my program. The application is in the form of a jar file. It can take a input from a txt file and spit out a text file as output. I ran this from command prompt and works fine. I need to use this in my program. specify the input txt file and run the jar file. Wait for it to complete and then read& display the output text file.
I really appreciate if you guys can show me how to do this
Question by:guyneo
  • 2
  • 2

Expert Comment

ID: 24245748
You could use the Runtime to launch the application as any other like:
Runtime r = Runtime.getSystemRuntime();
r.exec("java -jar path_to_the_jar");

Quite ugly but it works. A bit nicer solution would be to include the jar within your classpath and call the method of the project that you need here. You could eventually find the main method from this jar in the Manifest.

Author Comment

ID: 24251536
Great!. It looks like it is working. But how do I wait for it and detect when its done.. any idea?
 Also I used Runtime.getRuntime().exec instead of Runtime.getSystemRuntime(); Ic ould not find getSystemRuntime. is it because I am using windows?

Accepted Solution

__geof__ earned 2000 total points
ID: 24255188
No you are right, it is Runtime.getRuntime. A typo from my part.

Runtime.getRuntime().exec() returns a Process object and there is a method called waitFor() that stop thread until the process is finished.

Author Comment

ID: 24295120
Here is the code just for the benefit of those who might need it.

Process proc=  Runtime.getRuntime().exec("YOUR COMMAND ");
try {
            } catch (InterruptedException ex) {


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question