Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can Java execute batch file outside of current JVM in separate process tree

807606Apr 2 2007 — edited Apr 5 2007
Hi,

Does anyone know how to run programs from Java as separate processes that will not die when the spawning java program exits (JVM exits).

The problem I have with using Runtime.exec is it spawns only child processes under the current running JVM, thus when the origonal program that called Runtime.exec ends so does all child processes.

Basically I want to start a DOS batch file from my Java application, my Java application will then immediately exit (calling System.exit(0) ). The batch program will continue to run, its does some file clean up, create's some new files and deletes the old jar (containing the main app), it then rebuilds the main app jar and and executes the main class and then exits itself.

I've also tried the apache.tomcat.jni.Proc :-

long pool = Pool.create( new Long(0).longValue() );
long pa = Procattr.create( pool );
Procattr.dirSet( pa, "c:\\temp\\updater\\");
Procattr.cmdtypeSet( pa, Proc.APR_SHELLCM );
Procattr.detachSet( pa, 1 );
long proc = Proc.alloc( pool );
Proc.create( proc, "test.bat", new String[]{"test.bat"}, null, pa, pool );
System.exit(0);

The detach option doesn't work, if I take it off then the bat file runs and stops the JVM exiting, if I leave it in the batch file never gets called.

Is this possible in Java. Can java start master process on Windows XP JDK1.5+?

Cheers
Chris.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 2 2007
Added on Apr 2 2007
7 comments
2,807 views