java - How to Terminate a Process Normally Created using ProcessBuilder
I am creating Processes using ProcessBuilder in my Java Application. The created process executes some FFMPEG commands which actually copy the RTSP streams in specified destination media file.
ProcessBuilder builder = new ProcessBuilder("ffmpeg", "-i", RTSP_URL, "-f", fileFormat, destFilePath); Process processToExecute = builder.start();
I want to close the process before it completes its execution. So, If I run this FFMPEG command directly in windows CMD and then press 'CTRL+C' after 5 seconds then process get terminates with status '2'. And I can play the media file created so far.
So, If I do the same operation in my Java Application using:
process.destroy(); //I call this method after 5 sec
I get the status code '1' which means abnormal termination. I get the status by the following way:
processToExecute.destroy(); processToExecute.exitValue(); //This return me status '1'
And I can't play the media file and I think this is due to the abnormal termination of the process.
So how I can terminate the process created using ProcessBuilder in the same way we do in CMD with (CTRL+C) so that I may play the created media file ?
I want to terminate process (created using ProcessBuilder) in Java Application with status code of '2' that I get when I terminate process using CMD.
EDIT#01: --- Sharing Findings
So, when I try to delete that file once app terminates, I get the following error:
The Action Can't be Performed Because File is Opened in FFMPEG.exe
Which means that process is not terminating the command it is executing. That command still has occupied this file that's why I am not getting able to play it. Process gets terminate when I call:
But, the task it is performing (that is execution of a command) is still active. Strange!!!!
EDIT#02: Sharing Ultimate Reason
Actually If I directly press 'CTRL+C' or 'q' in cmd when process is running then it terminates the process successfully and this process is no more visible in the currently executing processes lists.
And Programatically when I call method:
It terminates the process but when I see the list of currently executing processes I can still see them over there.
And same scenario exists If I try to terminate this process using 'taskkill' or 'kill' command in another CMD by specifying their's name or pid that still process terminates abnormally.
P.S. I use the following command to see the running processes:
So from this it proves that destroy() method from Application and 'taskkill or kill' command from another CMD is not terminating the process normally that pressing 'CTRL+C' and 'q' does.java windows ffmpeg processbuilder kill-process
this question edited Jan 30 '15 at 10:57 asked Jan 14 '15 at 12:11 Bilal Ahmed Yaseen 367 1 3 16 I don't know if you can do this in Java, but based on your description, you need to send a control-C signal to FFMPEG. The Win32 API call to do this is GenerateConsoleCtrlEvent. Incidentally, the problem isn't the status code per se, that's just another symptom; terminating the process with status code 2 is no different to terminating it with status code 1. You have to ask it to terminate (rather than forcing it to do so) so that it can run its cleanup routines. – Harry Johnston Jan 14 '15 at 22:38 If it's ffmpeg, maybe send it a 'q' key to tell it to quit... – rogerdpack Jan 30 '15 at 11:22 yes If I send 'q' through cmd it does terminate process normally. But I want to terminate it from inside my Application. – Bilal Ahmed Yaseen Jan 30 '15 at 11:28 1 @BilalAhmedYaseen: Did you get a solution to this? I am getting the same issue. – ParagJ Feb 26 '15 at 9:06 I created a library do do video exporting ( github.com/hamoid/video_export_processing) and run into this exact issue on Windows. Would it be possible to send keystrokes to the process, as proposed at the bottom of stackoverflow.com/questions/3240704/… ? – aBe Aug 4 '16 at 12:48
Or you could try to write to the stdin of the process...
this answer edited Jan 14 '15 at 12:50 answered Jan 14 '15 at 12:30 Joe 102 2 There is no such 'inheritIO()' method for 'Process' class in Java. ProcessBuilder builder = new ProcessBuilder(command); Process processToExecute = builder.start(); – Bilal Ahmed Yaseen Jan 14 '15 at 12:33 Dear Again there is no 'write()' method on InputStream. We can just read from InputStream while we can Write on OutputStream. And with OutputStream It makes no sense. – Bilal Ahmed Yaseen Jan 14 '15 at 13:17
- 2Android APK反编译详解（附图）
- 3JAVA用TCP 实现反向连接屏幕监视
- 5JAVA 正则表达式 （超详细）