我用 java 开启多个 appium 服务,然后想关闭指定的端口下的服务该怎么做;
目前情况是命令已经知道但是在执行命令后没有一点反应,程序好像就是在 appium 服务界面一样无法输入任何命令,但我又不知道如何在命令行模拟 ctrl+c 的操作结束服务。
如果我在另外一个 main 函数执行杀进程的命令是可以结束的。
一直被困扰的一个问题望各位能给个解决的办法或者思路,不胜感激!

这是我启动服务的代码

package com.ben.test;

import java.io.*;

public class AppiumServer {
    public Process pc;
    Runtime rt;
    Thread thread1;
    Thread thread2;

    public void sunServer(String cmd) throws Exception {
        // TODO Auto-generated method stub
        rt = Runtime.getRuntime();
        String[] command = { "cmd", "/c", cmd };
        pc = rt.exec(command);
        thread1 = new Thread(new Input());
        thread1.start();
        thread1.sleep(500);
        thread2 = new Thread(new Output());
        thread2.start();
    }
    class Output implements Runnable {
        public void run() {
            OutputStream fos = pc.getOutputStream();
            PrintStream ps = new PrintStream(fos);

            ps.flush();

        }
    }
    class Input implements Runnable {

        public void run() {
            InputStream ios = pc.getInputStream();
            BufferedReader brd = new BufferedReader(new InputStreamReader(ios));
            String s;
            try {
                while ((s = brd.readLine()) != null) {
                    System.out.println(s);
                }
            } catch (Exception e) {
            }
        }
    }
}

这是测试代码

@Test
    public void test() {
        int port = 4723;
        int bp = port + 1;
        String cmd = "appium -p " + port + " -bp " + bp
                + " --session-override -a 127.0.0.1";
        try {
            AppiumServer as = new AppiumServer();
            as.sunServer(cmd);//开启服务
            Thread.sleep(5000);
            DesiredCapabilities capabilities = new Devices().getcapabilities(
                    "MYVDU15629008926", "taobao.apk");//参数设置
            AndroidDriver<?> driver1 = new AndroidDriver(new URL(
                    "http://127.0.0.1:4723/wd/hub"), capabilities);
            Devices.DeviceMonitor(driver1);//执行脚本
            cmd = "netstat -ano | findstr " + port + " > "+port+".txt for "
                    + "/f \"tokens=5\" %%a in (" + port
                    + ".txt) do Taskkill /f /IM %%a";
            Runtime.getRuntime().exec(new String[] {"cmd", "/c", cmd});//执行杀进程命令
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


↙↙↙阅读原文可查看相关链接,并与作者交流