• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

making a process user/system process

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
paddy Mahadeva Iyer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running a batch file is itself OS dependent.
 
paddy Mahadeva Iyer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic