windows - Java process is not terminating after starting an external process -
on windows i've started program "async.cmd" processbuilder
this:
processbuilder processbuilder = new processbuilder( "async.cmd" ); processbuilder.redirecterrorstream( true ); processbuilder.start();
then read output of process in separate thread this:
byte[] buffer = new byte[ 8192 ]; while( !interrupted() ) { int available = m_inputstream.available(); if( available == 0 ) { thread.sleep( 100 ); continue; } int len = math.min( buffer.length, available ); len = m_inputstream.read( buffer, 0, len ); if( len == -1 ) { throw new cx_internalerror(); } string outstring = new string( buffer, 0, len ); m_output.append( outstring ); }
now happened content of file "async.cmd" this:
rem start command window start cmd /k
the process started extenal program terminated (process.waitfor()
returned exit value). sent readerthread.interrupt()
reader thread , thread terminated, too.
but there still thread running wasn't terminating. thread kept java application running if exited main
method. debugger (eclipse) wasn't able suspend thread. after quit opened command window, java program exited, too.
question
how can quit java program while command window stays open?
you can use system.exit(0) method after know child process finished (e.g. after process.waitfor() returned exit value). method exit application anyway no regards background threads.
Comments
Post a Comment