aspose file tools*
The moose likes Java in General and the fly likes making a process user/system process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "making a process user/system process" Watch "making a process user/system process" New topic
Author

making a process user/system process

paddy Mahadeva Iyer
Ranch Hand

Joined: Feb 10, 2007
Posts: 63
Hi guyz,
I need some guidance! i have a java process being initated from a batch file!! but when ever i am closng the terminal on which the batch file is executing the java process also shutsdown!! i need it to run even after i have closed the terminal!!

is there anyway i can do this!!!

thnx in advance!
rgds paddy!!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
Welcome to JavaRanch.

If you're on a Unix-ish OS, check out the excellent "screen" utility. It allows you to decouple a terminal session from an actual terminal window, and can do several other nifty tricks as well.


Ping & DNS - my free Android networking tools app
paddy Mahadeva Iyer
Ranch Hand

Joined: Feb 10, 2007
Posts: 63
Hi,

Thanks for the reply. But i cannot let it to be OS dependend. Is there any piece of code that i can write which will help me avoid the shutting of java process when the terminal/cmd is closed?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
It WILL be OS-dependent. Unix terminals and the Windows console simply are not alike.

The question boils down to how a process is run on an OS, and that has nothing to do with Java, and everything to do with the OS.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Running a batch file is itself OS dependent.
paddy Mahadeva Iyer
Ranch Hand

Joined: Feb 10, 2007
Posts: 63
yup! i ahve .bat and .sh files for the windows and linux environment!

can somebody suggest what could be done if am on the windows environment. Say i close the cmd after executing the batch file and i do not want the java process to exit. My question is.. can i deal with this issue with a piece of code ?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In windows bat, I do this:

start javaw.exe MyProgram 1>myprogram.out 2>myprogram.err

The "start" command runs the rest of the command in a new process. Because javaw has no console, I redirect the outputs to files just in case something goes bad.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Provided the Java implementation on your platform has the relevant support (it should), you can use a very simple bootstrap static main method to load your program as a new (daemon) process in the OS. Look at java.lang.Process and java.lang.Runtime, and try running "java ..." as the execution command. The new process runs asynchronously in a new JVM, so closing the current JVM down (by exiting the bootstrap class) is no problem. Also, the new process isn't tied to the current screen, so it will continue to run even after you log off.

I've used this successfully on both Windows and Unix without changing the code between platforms: however, you may want to provide the execution syntax "java ..." as part of the bootstrap method's arguments from your batch file, so the program itself is devoid of OS dependency (e.g. location of installed JRE).

You'll probably also want to code someway to shutdown the service (from within the running process) and this is often done using an appropriate signal via sockets.

Alternatively, if you don't care about a neat shutdown, you should be able to kill the process at any time (using "kill" on Unix or the Task Manager on Windows).


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: making a process user/system process