Run Java console app as a daemon (background)

22,843

Solution 1

The answer is operating system dependent.

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

Solution 2

If you are doing this in anything unix based then you can append & to the end which will spawn a new thread and keept it running in the background.

java -jar myapp.jar &

Solution 3

If you really just want it to run in the background, java -jar myapp.jar & will do the job. That way, it'll still die when the shell closes, but you can keep using your shell.

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

If you want this to be reliable, you can prepare an init script or upstart job definition, or run it via Vixie cron(8) @reboot specifier to make it start at boot.

Solution 4

Given that you're using Windows, you might consider Java Service Wrapper. I have used it on a project in the past.

Share:
22,843
aleroot
Author by

aleroot

I used to hate C++ until I suddenly fell in love with it :-)

Updated on July 14, 2022

Comments

  • aleroot
    aleroot almost 2 years

    I've developed a Java console application that when start, open a console window and remain in foreground, i want to start that application in background .

    Now i launch the application by this command line :

    java -jar myapp.jar
    

    Is there a way to achieve this behaviour ? It's enough change the command line parameter or i need to do some change on my code ?

  • Jim
    Jim almost 13 years
    Additionally you can do "javaw" instead of "java" which will run your program without a console window. This is typically how a GUI based java program should be run. download.oracle.com/javase/6/docs/technotes/tools/windows/…
  • Kalyan
    Kalyan about 8 years
    Just want to point that this would be for an Unix or a Linux machine and not for windows.
  • Gary - Stand with Ukraine
    Gary - Stand with Ukraine almost 6 years
    at least until you log out. then it'll take the app with it. Use nohup in front