Java代码操作win后台进程

Java代码操作win后台进程

话不多说,直接上代码

强调说明:

使用的时候程序要有管理员权限,也就是以管理员身份运行,运行IDE,或是运行打包好的黑窗口。


/**
 * 代达罗斯之殇
 *
 * @ClassName: ProcessUtils
 * @Description: 关于Java代码操作电脑后台进程的工具
 * @author: zhangYuanLiang
 * @date: 2022/5/12 11:05
 */
public class ProcessUtils {

    /**
     * 结束进程
     */
    public static void killProcess(String pid) {
        Runtime runtime = Runtime.getRuntime();
        try {
            //taskkill /pid 10580 -f
            Process tasklist = runtime.exec("cmd.exe /c taskkill /f /t /im " + pid);
            System.out.println("进程关闭成功" + pid);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拿到PID对应的进程名
     * @return
     */
    public static String getPressName(String PID) {
        ProcessUtils stringUtils = new ProcessUtils();
        Runtime runtime = Runtime.getRuntime();
        String processName = null;
        int index = 0;
        try {
            Process tasklist = runtime.exec("tasklist");
            BufferedReader reader = new BufferedReader(new InputStreamReader(tasklist.getInputStream(), "GBK"));
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                if (index >= 3) {
//                    System.out.println(line);
                    String findpid = stringUtils.findpid(line);
                    if (PID.equals(findpid)) {
                        processName = stringUtils.getProcessName(line);
                    }
                }
                index++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return processName;
    }

    /**
     * 返回端口号对应的PID
     *
     * @param Port
     * @return
     */
    public static String getPID(String Port) {
        ProcessUtils stringUtils = new ProcessUtils();
        Runtime runtime = Runtime.getRuntime();
        String PID = null;
        try {
            Process findPort = runtime.exec("netstat -ano");
            BufferedReader reader = new BufferedReader(new InputStreamReader(findPort.getInputStream(), "GBK"));
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
                String ipAndPort = stringUtils.findIPAndPort(line);
                String ipAndPortE = stringUtils.findIPAndPortExternal(line);
                String port = stringUtils.findPort(ipAndPort);
                String portE = stringUtils.findPort(ipAndPortE);
                if (port.equals(Port)) {
                    PID = stringUtils.findPID(line);
                }
                if (portE.equals(Port)){
                    PID = stringUtils.findPID(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return PID;
    }





    /**
     * 根据PID拿到进程名称
     */
    public static String getProcessName(String line) {
        String ss = line;
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < ss.length(); i++) {
            if (ss.charAt(i) != ' ') {
                buffer.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return buffer.toString();
    }

    /**
     * 解析出PID
     */
    public static String findpid(String line) {
        String ss = line;
        StringBuffer buffer = new StringBuffer();
        for (int i = 33; i >= 0; i--) {
            if (ss.charAt(i) != ' ') {
                buffer.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return buffer.reverse().toString();
    }

    /**
     * 拿到IP
     */
    public static String findIPAndPort(String IP) {
        String ss = IP;
        StringBuffer ip = new StringBuffer();
        for (int i = 9; i < ss.length(); i++) {
            if (ss.charAt(i) != ' ') {
                ip.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return ip.toString();
    }

    /**
     * 拿到对外的ip
     * @param IP
     * @return
     */
    public static String findIPAndPortExternal(String IP) {
        String ss = IP;
        StringBuffer ip = new StringBuffer();
        for (int i = 32; i < ss.length(); i++) {
            if (ss.charAt(i) != ' ') {
                ip.append(ss.charAt(i));
            } else {
                break;
            }
        }
        return ip.toString();
    }

    /**
     * 拿到PID
     */
    public static String findPID(String PID) {
        String ss = PID;
        StringBuffer pid = new StringBuffer();
        for (int i = 71; i < ss.length(); i++) {
            pid.append(ss.charAt(i));
        }
        return pid.toString();
    }

    /**
     * 拿到端口号
     */
    public static String findPort(String ipAndPort) {
        String ip = ipAndPort;
        StringBuffer buffer = new StringBuffer();
        for (int i = ip.length() - 1; i >= 0; i--) {
            if (ip.charAt(i) != ':') {
                buffer.append(ip.charAt(i));
            } else {
                break;
            }
        }
        return buffer.reverse().toString();
    }

}

操作启动win程序

/**
 * 代达 罗斯之殇
 *
 * @ClassName: StringUtils
 * @Description: bat端口处理脚本
 * @author: zhangYuanLiang
 * @date: 2022/5/11 16:22
 */
public class UeUtils {

    /**
     * 启动指定端口号的ue实例
     * @param port 端口号
     */
    public static boolean startUE(String port) {
        try {
            String s = "cmd /c start E:\\NN" + port + "\\Windows\\NanNingTest_" + port + ".exe.lnk";
            String ss = "cmd /c start E:\\PixelStreaming\\WebServers\\SignallingWebServer" + port + "\\platform_scripts\\cmd\\run_local.bat";
            ExecuteScriptUtils.exCmd(ss);
            ExecuteScriptUtils.exCmd(s);
            return true;
        }catch (Exception e){
            return false;
        }

    }


    /**
     * 通过端口号来查杀进程, 杀死虚幻实例进程
     * @param port 端口号
     */
    public static boolean killProcessByPort(String port) {

        try {

            String pport = null;
            if (port.equals("31")){
                pport = "8001";
            }else if ("32".equals(port)){
                pport = "8002";
            }else if ("33".equals(port)){
                pport = "8003";
            }

            String pid = ProcessUtils.getPID(port);
            String pidp = ProcessUtils.getPID(pport);
            ProcessUtils.killProcess(pid);
            ProcessUtils.killProcess(pidp);

            ExecuteScriptUtils.closeCmd();
            return true;
        }catch (Exception e){
            return false;
        }

    }


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用户的测试机安装的win7,本人用的是 WEBLOGIC8.1 JDK1.4的。 测试时发现时间总是差8个小时,郁闷死。 立马想到时区不对,查看控制面板里时区设置(顺便BS一下win7,控制面板就不能学xp有个经典模式吗?郁闷死),发现时区设置正确,看来不是时区设置问题。 在win7下装JDK1.4和JDK1.5、eclipse,输出系统时区和时间。 果然,输出时区为"GMT",时间差8小时。那就说明是jdk1.5在win7下取不到正确的时区。 什么原因呢,为了验证我的假设,又下了最新的jdk1.6,后台打印输出正常。 问题找到了,接下来就要解决。为什么xp下能取到时区儿而win7下取不到呢? 搜索一下,到SUN论坛,发现SUN提到了这个bug,(http://java.sun.com/javase/tzupdater_README.html)而且发布了一个tzupdater。 按照使用说明下载、运行,再试,还是不行。 虽然用updater没有成功,但通过他的使用说明还是发现了一些东东。在/jdk1.5.0_04/jre/lib目录下有个tzmappings文件。 用记事本打开,里面记录了所有地区的时区,格式为“China Standard Time:-1,75::Asia/Shanghai:”,看到它,很自然的联想到注册表。 于是,打开注册表时区的节点[HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Time Zones/China Standard Time]。 有一个键值似曾相识,“MapID=-1,75”,这不就是tzmappings文件中记录的那个值吗。 会不会Java是通过这个键值获取的时区?展开win7下注册表相同的节点,我靠,里面竟然没有MapID键, 与xp对比了一下,少了MapID、Index键,多了MUI_Display、MUI_Dlt、MUI_Std啥的, 其他的不管,先把MapID键加上再说。加上后,再运行我那个取系统时间的测试程序,OK!问题解决! 本同目录下有两个注册表。一个是中国的 。一个是时区全的。那个都行。 其实本人也是参考CSDN部分帖子,再有疑问联系,CSDN:lawsystem 感谢 CSDN 无名贡献者。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值