简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM