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.
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...
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?
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 System.in, 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.
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 ]
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.