Link to home
Start Free TrialLog in
Avatar of guyneo
guyneo

asked on

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
Avatar of __geof__
__geof__
Flag of Norway image

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.
Avatar of guyneo
guyneo

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of __geof__
__geof__
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guyneo

ASKER

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

Process proc=  Runtime.getRuntime().exec("YOUR COMMAND ");
try {
                proc.waitFor();
            } catch (InterruptedException ex) {
               System.err.println("Error!!!"+ex.getLocalizedMessage());

            }