Reputation: 995
Suppose I have a class which has a main method like below and that hoge.hoge()
takes long time to complete.
public static void main(String[] args){
hoge.hoge();
}
This application will be executed from the command line. I would like the application to immediately become a background task, exactly as if someone had executed:
java AboveClass &
I.e. the user should be returned back to the prompt. Is there a way I can achieve this in Java?
Upvotes: 0
Views: 187
Reputation: 4314
Unfortunately, no, there is no such way to make the task into a background task. You could try creating a thread (and make it a daemon thread), but I think you'll find that even then if you return from the main method and the thread is still running, the program does not return to the prompt.
@Duncan suggested supplying a script that your users can run, which starts the Java program in the background. I recommend that approach.
If you want an all-Java solution, you could try creating the script in Java. See How to execute system commands (linux/bsd) using Java for how to execute commands from a Java program. So, you would execute "java ClassName &" from the Java program then. Not the prettiest solution, but it works. Of course it requires that the "java" command is in the path.
Upvotes: 1