Sunteți pe pagina 1din 4

7/14/13

Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Engineering Linux / UNIX

Author

Running java applications in virtual machine without a terminal


posted 8/20/2004 10:11 PM

S. Lohi Greenhorn Joined: May 21, 2004 Posts: 11

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 Joined: Aug 02, 2004 Posts: 823

posted 8/20/2004 10:42 PM

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

www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine

1/4

7/14/13

Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)

after your terminal session ends. I'm not sure where the output (stdout/stderr) would go though... Jules

S. Lohi Greenhorn Joined: May 21, 2004 Posts: 11

posted 8/20/2004 11:41 PM

Thanks, I'll see what I can work out with that.

Tom Hill Ranch Hand Joined: Aug 24, 2003 Posts: 115

posted 8/21/2004 1:13 AM

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 Joined: Jul 08, 2003 Posts: 24093

posted 8/21/2004 1:42 AM

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.

16
I like...

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 byebye. 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

www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine

2/4

7/14/13

Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)

the keystrokes might be slightly different. Tom Nope.

[Jess in Action][AskingGoodQuestions]

S. Lohi Greenhorn Joined: May 21, 2004 Posts: 11

posted 8/21/2004 2:02 AM

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 Joined: Aug 02, 2004 Posts: 823

posted 8/21/2004 2:24 AM

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 Joined: Dec 10, 2001 Posts: 7023

posted 8/21/2004 3:48 AM

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

posted 8/21/2004 4:47 AM

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

www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine

7/14/13

Running java applications in virtual machine without a terminal (Linux / UNIX forum at JavaRanch)

Posts: 1923
I like...

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.

http://home.arcor.de/hirnstrom/bewerbung

S. Lohi Greenhorn Joined: May 21, 2004 Posts: 11

posted 8/21/2004 2:42 PM

Stefan:

Thanks, that did it

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Running java applications in virtual machine without a terminal

Similar Threads Maintaining session in a terminal services environment PuTTy backspace addShutdownHook (on Windows) Java program terminates Head First Java book, chapter 15 - help needed with running the chat terminals
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:37:01 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/110369/Linux-UNIX/Running-java-applications-virtual-machine

4/4

S-ar putea să vă placă și