Skip to main content

how to detach child from process tree on win32?

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
aefremov
Offline
Joined: 2007-10-01

Hello,

The basic idea is to call exec and let process run.
1) I tried with processBuilder.start
2) I tried with apache tcnative
long p = Pool.create(0);
long pa = Procattr.create(p);
Procattr.cmdtypeSet(pa, Proc.APR_PROGRAM);
Procattr.ioSet(pa, 0, 0, 0);
Procattr.detachSet(pa, 1);
long proc = Proc.alloc(p);
long prog =
Proc.create(proc, "D:\\ProgramFiles\\jdk1.6.0_06\\bin\\javaw.exe", new String[] {
"D:\\ProgramFiles\\jdk1.6.0_06\\bin\\javaw.exe", "-classpath",
"F:\\projets\\....", Fake.class.getName() }, null, pa, p);

Result: Program gets executed and the launcher terminates. but eclipse hangs up waiting for child (Fake.main).
If I kill it in program manager eclipse shows that all is complete.

On linux there is no problem at all. just working system. But on windows I have no idea how to cut off link with parent process tree.

Launcher must spawn the server program and let it run forever. But program that calls launcher has to see that launcher is terminated.
In my case eclipse doesnt. It waits for all child programs terminates also.

many thanks,
alexander efremov

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aefremov
Offline
Joined: 2007-10-01

Hello,

I've found solution = to detach new process from parent's console, to create another one and set hide flag thus user can't even see that new console has been created. It works fine.
Any feedback?
alexander efremov

public static void main(String[] args) throws Exception{
String cmd = System.getProperty("java.home")+
System.getProperty("file.separator")+
"bin"+
System.getProperty("file.separator")+
"java.exe"+
" -classpath "+
System.getProperty("java.class.path")+
" "+Fake.class.getName();

STARTUPINFO lpStartupInfo =new STARTUPINFO();
lpStartupInfo.dwFlags=0x00000001;

PROCESS_INFORMATION lpProcessInformation =new PROCESS_INFORMATION();
Kernel32 kernel=(Kernel32) Native.loadLibrary("Kernel32",Kernel32.class);
kernel.CreateProcessA(null,cmd,null, null, false, 0x00000010 , null, null, lpStartupInfo, lpProcessInformation);
}
Or u can go with Detach flag and javaw.exe

kernel.CreateProcess(null,cmd,null, null, false,DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT, env.toString(),null,lpStartupInfo,lpProcessInformation))

Main idea is to kill console handle otherwise inheritable by child process. Setting inheritable to true kill independence.
Windows is very strange system. Either you inherit all handles or nothing.

First step is to create launcher process that inherit nothing. Then launcher spawns a server process and waits for ready signal from server's stdout. then exits.
Program that runs launcher will notice that. (p.waitFor)