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
2

1 Answer 1

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");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
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?
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.
Thnaks for your reply, first i see multi threading then i come back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.