Run the Java console application as a daemon (background)

I developed a Java console application that, when launched, opens a console window and remains in the foreground, I want to run this application in the background.

Now I run the application on this command line:

java -jar myapp.jar 

Is there any way to achieve this behavior? Is it enough to change the command line parameter or do I need to make some changes in my code?

+6
source share
4 answers

The answer depends on the operating system.

 *nix: <your command> & Windows: (opens a new console): start <your command> Windows: (doesn't open a new console): start /b <your command> 
+12
source

If you do this anyway, based on unix, you can add to the end, which will spawn a new thread and start it in the background.

 java -jar myapp.jar & 
+6
source

If you really want it to run in the background, java -jar myapp.jar & will do the job. That way, he will still die when the shell closes, but you can continue to use your shell.

If you really want it to run as a daemon, nohup java -jar myapp.jar & will do the job. Thus, he will continue to live when the shell closes.

If you want this to be reliable, you can prepare an init script or definition for the upstart job, or run it via Vixie cron(8) @reboot so that it starts at boot time.

+4
source

Given that you are using Windows, you might consider Java Wrapper . I have used it in the past in the past.

+3
source

Source: https://habr.com/ru/post/891316/


All Articles