无法停止在线程中启动的命令行进程(在javafx中)

时间:2017-02-10 06:44:28

标签: java multithreading javafx cmd process

我在线程中使用Java进程构建器启动了命令行perl进程。当我关闭我的javafx应用程序时,perl.exe进程一直在运行。

我想关闭cmd进程的输入流并停止该线程。

我已尝试过以下步骤:

  1. System.exit
  2. process.getInputStream.close()
  3. 还尝试使用java cmd进程(taskkill / f / im perl.exe)杀死进程
  4. 代码:

    MyRunnable CLASS:

        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)
                   {    
                   }
                 }
               }
    

    主要课程:

    两个按钮:

    播放:

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

    STOP BUTTON:

        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
    

2 个答案:

答案 0 :(得分:1)

ProcessBuilder start()方法返回一个Process对象。

具有 destroy() 方法。简单地称之为!

答案 1 :(得分:1)

由于java线程启动另一个进程,因此在调用destroy后它不会被杀死。以下命令摆脱了所有 perl进程,从而解决了问题:

ProcessBuilder pb3 = new ProcessBuilder(new String[] { "cmd.exe", "/c", "taskkill /im \"perl.exe\" /f"}); 
Process pr13=pb3.start();