sjain
sjain

Reputation: 1725

how to run a java program from another java program independently?

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

Upvotes: 0

Views: 771

Answers (1)

Bart Vangeneugden
Bart Vangeneugden

Reputation: 3446

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");
    }
}

Upvotes: 1

Related Questions