How do I make a background thread in Java that allows the main application to exit completely? This works in Linux, but not in Windows

Bob picture Bob · Apr 15, 2010 · Viewed 9.1k times · Source

I have a Java application that creates a new thread to do some work. I can launch the new thread with no problems.

When the "main" program terminates, I want the thread I created to keep running - which it does...

But the problem is, when I run the main application from Eclipse or from Ant under Windows, control doesn't return unless the background process is killed.

If I fork the main java process in ant, I want control to return to ant once the main thread is done with its work... But as it is, ant continues to wait until both the main process and the created thread are both terminated.

How do I launch the thread in the background such that control will return to ant when the "main" application is finished? (By the way, when I run the same application under Linux, I am able to do this with no problems).

Answer

BalusC picture BalusC · Apr 15, 2010

Your best bet is to launch a completely independent program for that, which is independent from the launching program. You can do that with Runtime#exec(), ProcessBuilder or Desktop#open().