How to run a java program in backgroud (unix / Linux)
Oftentimes, we use SSH to remote access into the server to run a Java program. The problem is, we can’t type anything After the Java program is executed like this :
$ java -jar example.jar
In addition, when the remote access session is expired or terminated, the executed Java program will be killed.
Solution
To fix it, append a &
symbol to the end of the command, it executes the Java program in the background, and continue until it finished.
$ java -jar example.jar &
If the above command get killed when you leave the SSH session, try to add nohup
in front.
$ nohup java -jar example.jar &
How to run it using from jsp
Thanks a ton !!!…Really helpful
I know this was posted long ago, sorry about commenting on it.
I’ve got an issue related to this, driving me crazy. When I run my jar on linux prompt with a command like this:
java -jar MyJar.jar &
the process starts running in background as expected, but this process always gets killed some time later, say 2 hours for a reason I can’t figure out; Not memory issues because I’ve tried VM option (Xmx) and never getting the exception. Also no logs found in kernel about it.
But if I start app without the & sign and keep the console open, it will continue running as long as I have the console opened.
Does this mean anything to you? Any suggestion please.
Try nohup
nohup java -jar example.jar &
It still does not work for me.
pid gets created and it runs once .then when i do ps-fp ,it does not give any process
Even though the post is old. Maybe you run it as a different user and then you exit, or if not, you need to do the following nohup java -jar MyJar.jar &
Very nice mkyong. It worked for me.
If i close my SSH like Putty, will it stop the process?
No, the command will still running on your server.
I think it not right. I tried and close Putty, the pricess also be closed.
Try nohup
nohup java -jar example.jar &
Really useful, you save my life!
any body tell me how to do it in windows xp……….
On Windows, try task scheduler
>javac filename.java
>java filename
I think I will give this a try, thanks for the post.
Thanks! Exactly what I was looking for.
tnx.. i found 1 of my assignments…
can you help me some times if i have my other assignments?…
Assignment? Try send me an email
Interesting,
Although linux is not so widely used ,
Keep up the good work
Is it? Linux is not widely use in UK? However, this is very common and widely use in open source development environment