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

Posted on 2009-04-27
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
    LVL 6

    Expert Comment

    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.
    LVL 1

    Author Comment

    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?
    LVL 6

    Accepted Solution

    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.
    LVL 1

    Author Comment

    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    A short article about problems I had with the new location API and permissions in Marshmallow
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now