1

I am trying to launch multiple links using java, but firefox is giving an error

Firefox is already running please close first

So to avoid this, I added a delay between launching links. but this delay is blocking my main program. I made this piece of code as thread so that main program does not block, But exiting main program causes this thread to terminate without sleeping. This is my piece of code

main{
runCommand run= new runCommand();
run.start();
}

private class runCommand extends Thread{
        @Override
        public void run() {         
            LaunchProcess("xdg-open https://www.google.com")
            Thread.sleep(8000);
            LaunchProcess("xdg-open https:www.gmail.com")

        }

LaunchProcess is a function that uses runtime.getExec to exec command. the above code launches only the first link and exits as the main program exits. How to make sure that exiting main program does not terminate the threads launched by it. I dont want to add sleep in main program

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
chenna
  • 159
  • 1
  • 4
  • 14
  • http://stackoverflow.com/questions/22390977/allow-java-to-end-with-main-without-closing-processes-that-were-executed – assylias Oct 14 '15 at 12:31
  • That can't be your code, it's not valid Java syntax even if one adds the missing `LaunchProcess`. Also, there is no standard package or object `runtime`; there is `java.lang.Runtime` which has methods `getRuntime()` and `exec()` but no `getExec`. But what you describe should work; returning from the main thread in Java does not terminate the JVM if any other non-daemon thread still exists. Post actual code, as simple as possible, that demonstrates your problem. – dave_thompson_085 Oct 16 '15 at 06:32

1 Answers1

0

What you want is join()

The join method allows one thread to wait for the completion of another.

Read more about join() here.

Ghazanfar
  • 1,419
  • 13
  • 21
  • I want the main thread to quit and the thread launched by it should execute independently, is this possbile in java ? – chenna Oct 14 '15 at 12:23
  • if the main thread quits, the VM process quits, with all threads. however, join() will make the main thread sleep until the invoked thread is finished. isn't that what you want? – Sharon Ben Asher Oct 14 '15 at 12:26