This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Running java applications in virtual machine without a terminal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Running java applications in virtual machine without a terminal" Watch "Running java applications in virtual machine without a terminal" New topic

Running java applications in virtual machine without a terminal

S. Lohi

Joined: May 21, 2004
Posts: 11

I have this problem with my java application(s). I have them on my server machine that runs Linux. I connect to it using PuTTY. However, when I start up a java program it takes over the PuTTY terminal and is automatically terminated when I close the connection.

So, is there a way to run a java application in the virtual machine without the output terminal, or make it so that it isn't terminated when the terminal is shut down? Any help is appreciated.

- Sampsa Lohi
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Try using Thread.currentThread().setDaemon(true). This is similar to using nohup to detach a Unix process from the shell that kicked it off.

You may need to create a worker thread to do that, rather than using the main thread. The JVM will then terminate and you daemon thread will persist even after your terminal session ends. I'm not sure where the output (stdout/stderr) would go though...

S. Lohi

Joined: May 21, 2004
Posts: 11
Thanks, I'll see what I can work out with that.
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
I might as well as a similar question here, its along the same lines - lets assume that youve got some application that does loads of stuff - is it possible to 'mimic' closing the application - and then 'restart' it - without the process stopping. I guess its like getting the operating system to keep a reference to your application in memory or something?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Julian --

Actually, the purpose of "setDaemon(true)" is almost the exact opposite of what you've described: it arranges things so that the thread in question won't keep the JVM from exiting. If at any time, all the threads running in a JVM are daemon threads, the JVM will exit.

There's no way to arrange things so that any thread, daemon or not, keeps running after the JVM exits. Once the JVM process is gone, all threads go bye-bye.


You have to run the program "in the background"; when you start the process, type "java Whatever &" -- note that ampersand (&) at the end, which runs the program as a background process. Note that if the background process tries to read from, though, it will freeze.

Alternatively, you can send a foreground process into the background using the "^Z" character -- type control-Z to stop the process and get back to the shell prompt, and then type "bg" at the prompt to move the program into the background.

Anyway, once the program is running in the background, you can exit PuTTY and the program will continue to run.

This isn't Java-specfic at all: it's just how Linux (UNIX) works. I've assumed here that you're using bash, the standard Linux shell; if you're using a different shell the keystrokes might be slightly different.

Tom -


[Jess in Action][AskingGoodQuestions]
S. Lohi

Joined: May 21, 2004
Posts: 11

Thanks for the tip, I managed to make the program run in the background, but it still requires the PuTTY connection to run. For example, when I have the program running in the background and type 'logout' in the terminal, it clears the screen but the window stays open. When I close it, the program is terminated.

Thanks anyway, good to know how to run programs in the background

Edit: It seems that the program still uses the open PuTTY console for output for some reason. However, I was able to execute other shell commands after breaking the execution and typing 'bg' to move the program to the background. Beats me...
[ August 20, 2004: Message edited by: S. Lohi ]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Ernest,

Thanks for the clarification. I did think about that as I wrote it - i.e. how could a Java thread run without a JVM; just thought it might spawn a new one... Getting daemon threads confused with daemon processes. Dim of me.

So if the main thread (and any non-daemon threads) dies then the JVM exits and all the daemon threads die with it; I can see the sense in that.

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Linux / UNIX forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I heard of an application called 'screen' to start programs, log off, and later login, and join the 'screen' with the running program again.

I don't know how it works, how to setup, and didn't test it myself.
But it might be what you're looking for.
S. Lohi

Joined: May 21, 2004
Posts: 11

Thanks, that did it .
I agree. Here's the link:
subject: Running java applications in virtual machine without a terminal
Similar Threads
addShutdownHook (on Windows)
PuTTy backspace
Maintaining session in a terminal services environment
Head First Java book, chapter 15 - help needed with running the chat terminals
Java program terminates