通过java代码启动Windows应用,Runtime.getRuntime().exec()

一,通过java启动windows应用

实际用的是java的Runtime.getRuntime().exec()方法,exec实际运行目录是在开始-运行里,如已安装notpad++要启动的话如下

Runtime.getRuntime().exec("notpad++");

如果不是,则使用文件全路径"D:\MySoftware\CloudMusic\cloudmusic.exe",可在桌面快捷方式-打开文件地址获取
思路:使用Runtime.getRuntime().exec()运行open命令,运行完成后查询对应进程是否启动,以此判断是否启动成功

 /**
     * 传入启动应用路径,运行open命令
     *
     * @param filePath
     * @return
     */
    public static boolean startApp(String filePath) {
        String pocessName = new File(filePath).getName();
        logger.info("--启动 pocessName==="+pocessName);
        String result = "";
        if (filePath.contains(" ")) {
            // 由于有些文件名中间有空格,得把空格特殊处理
            for (int i = 0; i < filePath.length(); i++) {
                if (filePath.charAt(i) == ' ') {
                    result = filePath.replace(" ", "\\ ");
                }
            }
        } else {
            result = filePath;
        }
        try {
            logger.info("启动路径:" + result);
            String[] cmds = {"/bin/sh", "-c", "open -a " + result};
            Runtime.getRuntime().exec(cmds);
            logger.info("启动完成,等待10秒");
            Thread.sleep(10000);

            logger.info("等待完成,查看进程是否存在");
            boolean b = findProcess(filePath);
            if (b){return true;}

        } catch (Exception ex) {
            logger.info(ex);
        }
        logger.info("******************进程不存,启动失败");
        return false;
    }

三,查询windows进程,进程存在返回true

    /**
     * 传入进程名称processName,判断是进程是否存在
     * @param processName:进程名
     */
    public static boolean findProcess(String processName) {
        logger.info("查找进程" + processName);
        BufferedReader bufferedReader = null;
        try {
            Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName + '"');
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(processName)) {
                    logger.info(processName+"进程存在");
                    return true;
                }
            }
            logger.info(processName + "进程未启动");
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception ex) {
                    logger.info(ex);
                }
            }
        }
    }

四,关闭进程

传入进程名称

    /**
     * 传入进程名称,关闭进程
     * @param processName
     */
    public static boolean killProcess(String processName) {
        BufferedReader brStd = null;
        BufferedReader brErr = null;
        try {
            if (processName != null) {
                Process pro = Runtime.getRuntime().exec("c:\\windows\\system32\\taskkill /F /im " + processName);
                brStd = new BufferedReader(new InputStreamReader(pro.getInputStream()));
                brErr = new BufferedReader(new InputStreamReader(pro.getErrorStream()));
                long time = System.currentTimeMillis();
                while (true) {
                    if (brStd.ready()) {
                        logger.info("killProcess()进程正常返回:" + processName);
                        break;
                    }
                    if (brErr.ready()) {
                        logger.info("killProcess()进程出错返回:" + processName);
                        break;
                    }
                    if (System.currentTimeMillis() - time > 3000) {
                        logger.info("killProcess()等待超时:" + processName);
                        return false;
                    }
                }
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            //关闭流
            try {
                if (brErr != null) {
                    brErr.close();
                }
            } catch (IOException ex) {
                throw new RuntimeException("释放资源失败");
            } finally {
                try {
                    if (brStd != null) {
                        brStd.close();
                    }
                } catch (IOException ex) {
                    throw new RuntimeException("释放资源失败");
                }
            }
        }
        return true;
    }

五,复制文件,复制文件不能直接用copy,会提示copy文件找不到,用如下则可正常运行

把xxx.txt复制到D盘下

   public static void copyTxt(){
        File file = new File("");
        try{
            String filePath = file.getCanonicalPath();
            String copuDom = "copy "+ filePath+"\\src\\main\\resources\\file\\XXX.txt D:\\";
            System.out.println(copuDom);
            Runtime.getRuntime().exec(new String[]{"cmd", "/C", copuDom});
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值