Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running java applications in virtual machine without a terminal

 
S. Lohi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Jules
 
S. Lohi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll see what I can work out with that.
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

S.,

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.

Tom -

Nope.
 
S. Lohi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest:

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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Jules
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Linux / UNIX forum...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan:

Thanks, that did it .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic