0

I am running the code below. this runs the two instances of validity.jar as threads and main is not completed. I want to complete the main methods execution with out waiting for the completion of validity.jar. How can i do this?

try
{         
    int counter = 0;
    while(counter <2)
    {
        counter++;
        java.lang.Runtime.getRuntime().exec("java -jar C:\\validity.jar");
    }            
}   // try
catch(Exception e)
{
    e.printStackTrace();
}   // catch
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
sjain
  • 1,635
  • 9
  • 25
  • 35
  • Read this http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html – PeterMmm Jul 20 '11 at 10:04
  • are you meaning something similair as my question [Pass String as params from one Java App to another][1] [1]: http://stackoverflow.com/questions/6121990/pass-string-as-params-from-one-java-app-to-another – mKorbel Jul 20 '11 at 10:49

1 Answers1

1

Couldn't you add the jar file to the build-path of your project?

Then make a Thread or Runnable of your main class and start that twice?

public class SomeThread extends Thread {
    @Override
    public void run(){
        try {
            System.out.println("Starting thread");
            SomeThread.sleep(10000);
            System.out.println("Done thread!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        System.out.println("Start of main");
        Thread t = new SomeThread();
        t.start();
        System.out.println("Finished main");
    }
}
Bart Vangeneugden
  • 3,436
  • 4
  • 33
  • 52
  • Actually I want that main should not wait for the command that i run using the exec(). for example if i execute a jar file from my main() then main() should get finished and should not wait for the jar to get finished. If i make a thread main will wait untill all the threads are completed – sjain Jul 20 '11 at 11:19
  • @sjain sorry,but that's not correct. Your main will finish quietly while your threads still run. Try executing the code above, it will clear things up. Threads are threads so they don't have to wait on each other. – Bart Vangeneugden Jul 20 '11 at 11:50
  • If a am executing any command related to java (say java -jar) then main thread is waiting to child thread complete; but when I start a browser(exe of other commands) then main threads is finished. why? – sjain Jul 20 '11 at 12:40
  • 1
    Hm, could be that the `.exe` of starting your browser simply spawns another thread (the browser) and then finishes. Once finished, your Java thread can continue. Multithreading isn't that hard, and very interesting to read up on. – Bart Vangeneugden Jul 20 '11 at 13:00
  • Thnaks for your reply, first i see multi threading then i come back – sjain Jul 21 '11 at 04:10