如何终止通常使用 ProcessBuilder 创建的进程 [英] How to Terminate a Process Normally Created using ProcessBuilder

查看:107
本文介绍了如何终止通常使用 ProcessBuilder 创建的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

我想在进程完成之前关闭它.因此,如果我直接在 Windows CMD 中运行此 FFMPEG 命令,然后在 5 秒后按CTRL+C",则进程 get 以状态2"终止.我可以播放目前创建的媒体文件.

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.

因此,如果我使用以下方法在 Java 应用程序中执行相同的操作:

So, If I do the same operation in my Java Application using:

 process.destroy(); //I call this method after 5 sec

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

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.

那么我如何以与在 CMD 中使用(CTRL + C)相同的方式终止使用 ProcessBuilder 创建的进程,以便我可以播放创建的媒体文件?

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 ?

我想终止 Java 应用程序中的进程(使用 ProcessBuilder 创建),状态码为2",这是我使用 CMD 终止进程时得到的.

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.

因此,当我尝试在应用程序终止后删除该文件时,出现以下错误:

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!!!!

实际上,如果我在进程运行时直接在 cmd 中按 'CTRL+C' 或 'q' 那么它会成功终止该进程并且该进程在当前正在执行的进程列表中不再可见.

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.

如果我尝试在另一个 CMD 中使用 'taskkill' 或 'kill' 命令通过指定仍然异常终止的进程的名称或 pid 来终止此进程,则存在相同的情况.

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

因此,由此证明应用程序中的 destroy() 方法和另一个 CMD 中的taskkill 或 kill"命令不会像按CTRL+C"和q"那样正常终止进程.

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.

推荐答案

也许试试...

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

或者你可以尝试写入进程的标准输入...

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

process.getInputStream().write(exitCode);

这篇关于如何终止通常使用 ProcessBuilder 创建的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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