我已经在线程中使用 Java 进程构建器启动了命令行 Perl 进程。当我关闭 javafx 应用程序时,perl.exe 进程继续运行。
我想关闭 cmd 进程的输入流并停止该线程。
我已经尝试过以下步骤:
- 系统退出
- process.getInputStream.close()
- 还尝试使用 java cmd 进程终止进程 (taskkill/f/im perl.exe)
代码:
MyRunnable 类:
public class MyRunnable implements Runnable
{
private final int value;
private Boolean stop = false;
MyRunnable(int value)
{
this.value = value;
}
@Override
public void run()
{
try
{
ProcessBuilder pb2 = new ProcessBuilder(new String[] { "cmd.exe", "/c", "perl E:\\PerlRunner\\SCRIPTS\\AIRPLANE.pl SP9830A510159222101"});
Process pr12=pb2.start();
Thread.sleep(1000);
BufferedReader brq = new BufferedReader(new InputStreamReader(pr12.getInputStream()));
while(brq.readLine()!=null && Variables.flag )
{
System.out.println(brq.readLine()+" FLAG:IN WHILE"+Variables.flag);
}
if(!Variables.flag)
{
pr12.destroy();
System.out.println("PROCESS DESTOYED:"+Variables.flag);
}
}
catch(IOException ee)
{
}
catch(InterruptedException rr)
{
}
}
}
主要类(class):
两个按钮:
玩:
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Variables.flag=true;
System.out.println("Start");
MyRunnable n1= new MyRunnable(0);
Thread t1=new Thread(n1);
t1.start();
}});
停止按钮:
btn1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Variables.flag=false;
}});
Variables.flag 是在另一个类中定义的静态变量。
Perl 进程:
#!/usr/bin/perl
$devices = $ARGV[0];
$temp = 0;
$LOGFILE = "E:\\PerlRunner\\times.txt";
open(LOGFILE) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
if (index($line, "flmTimes") != -1)
{
my $result = index($line, "=");
$temp = substr($line, $result + 1);
}
sleep(1);
}
for($i=1; $i<=$temp;$i++)
{
system("adb -s $devices shell am start -a
android.settings.AIRPLANE_MODE_SETTINGS");
sleep 5;
system("adb -s $devices shell input tap 200 200");
sleep 35;
system("adb -s $devices shell input keyevent KEYCODE_HOME");
sleep 3;
}
times.txt :
testTimes=1
flmTimes=3
mtcTimes=1
caseTimes=1
最佳答案
关于java - 无法停止在线程中启动的命令行进程(在javafx中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42153069/