Main Tutorials

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 &

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
21 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Anonymus
3 years ago

How to run it using from jsp

Vikas
6 years ago

Thanks a ton !!!…Really helpful

Rakh
10 years ago

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.

mkyong
9 years ago
Reply to  Rakh

Try nohup

nohup java -jar example.jar &

snrat
6 years ago
Reply to  mkyong

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

arche
10 years ago
Reply to  Rakh

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 &

Lindsay
11 years ago

Very nice mkyong. It worked for me.

Freakygeek
11 years ago

If i close my SSH like Putty, will it stop the process?

Giang
11 years ago
Reply to  mkyong

I think it not right. I tried and close Putty, the pricess also be closed.

mkyong
9 years ago
Reply to  Giang

Try nohup
nohup java -jar example.jar &

Cesarla
11 years ago

Really useful, you save my life!

cracker
13 years ago

any body tell me how to do it in windows xp……….

mkyong
9 years ago
Reply to  cracker

On Windows, try task scheduler

suhana
12 years ago
Reply to  cracker

>javac filename.java
>java filename

Linh Locklier
13 years ago

I think I will give this a try, thanks for the post.

Mangara
13 years ago

Thanks! Exactly what I was looking for.

jeff
14 years ago

tnx.. i found 1 of my assignments…
can you help me some times if i have my other assignments?…

software development company
14 years ago

Interesting,

Although linux is not so widely used ,

Keep up the good work