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

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

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
  • 2
  • 2
1 Solution
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.
guyneoAuthor Commented:
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?
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.
guyneoAuthor Commented:
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) {

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.

Join & Write a Comment

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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now