200新新新新旗新新旗新200新新旗新新200新旗新新旗新200新新旗200新新旗200新新款200 [英] How to Terminate a Process Normally Created using ProcessBuilder

查看:213
本文介绍了200新新新新旗新新旗新200新新旗新新200新旗新新旗新200新新旗200新新旗200新新款200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Java应用程序中使用ProcessBuilder创建进程。创建的进程执行一些实际复制指定目标媒体文件中的RTSP流的FFMPEG命令。

  ProcessBuilder builder = new ProcessBuilder ffmpeg,-i,RTSP_URL,-f,fileFormat,destFilePath); 
进程processToExecute = builder.start();

我想在完成执行之前关闭该过程。所以,如果我在Windows CMD中直接运行这个FFMPEG命令,然后在5秒钟后按CTRL + C,那么进程将以状态2终止。我可以播放到目前为止创建的媒体文件。



所以,如果我在我的Java应用程序中使用相同的操作:

  process.destroy(); //我在5秒后调用这个方法

我得到状态代码'1',这意味着异常终止。我通过以下方式获得状态:

  processToExecute.destroy(); 
processToExecute.exitValue(); //这会返回我的状态'1'

我无法播放媒体文件,我认为这是由于进程的异常终止。



那么我如何能够以与CTRL + C在CMD中一样的方式终止使用ProcessBuilder创建的进程。所以我可以播放创建的媒体文件?



我想在Java应用程序中终止进程(使用ProcessBuilder创建),状态代码为2,当我得到使用CMD终止进程。



编辑#01:---分享结果



所以,当我尝试删除该文件一旦应用程序终止,我得到以下错误:

 由于文件在FFMPEG中打开,无法执行该操作.exe 

这意味着进程没有终止正在执行的命令。该命令仍然占用了这个文件,这就是为什么我无法播放它。当我打电话时,进程终止:

  processToExecute.destroy(); 

但是,它正在执行的任务(即执行命令)仍然处于活动状态。奇怪!!!!



编辑#02:分享终极原因



其实如果我直接按CTRL + C或q,当进程正在运行时,它会成功终止进程,并且此进程在当前执行的进程列表中不再可见。



并且程序化我调用方法:

  cmd> processToExecute.destroy(); 

它终止进程,但是当我看到当前正在执行的进程的列表,我仍然可以看到它们。



同样的场景存在如果我尝试通过指定他们的名字或pid异常终止,在另一个CMD中使用'taskkill'或'kill'命令终止此进程。



PS我使用以下命令查看正在运行的进程:

 任务列表
解决方案

也许尝试...

  builder.inheritIO(); 
System.exit(2);

或者您可以尝试写入过程的标准...

  process.getInputStream()。write(exitCode); 


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:

 processToExecute.destroy();

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:

cmd> processToExecute.destroy();

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:

tasklist

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.

解决方案

Maybe try...

builder.inheritIO();
System.exit(2);

Or you could try to write to the stdin of the process...

process.getInputStream().write(exitCode);

这篇关于200新新新新旗新新旗新200新新旗新新200新旗新新旗新200新新旗200新新旗200新新款200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆