java如何调用windows系统cmd

java如何调用windows系统cmd

1、多条命令之间用&连接

        // Java调用 dos命令
        try {
            Process process = Runtime.getRuntime().exec("cmd /c d: & cd 体检系统/ & kr_pe 1;"+regno);
        } catch (IOException e) {
            e.printStackTrace();
        }

2、将结果输出到idea,避免乱码

执行命令后结果输出到idea,若出现乱码请查看cmd的编码是什么

		// Java调用 dos命令
        String cmd = "ping www.baidu.com";
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            InputStream is = process.getInputStream();
            //此处加了编码方式,避免了乱码
            InputStreamReader isr = new InputStreamReader(is,"GBK");
            BufferedReader br = new BufferedReader(isr);
            String content = br.readLine();
            while (content != null) {
                System.out.println(content);
                content = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

调用结果

posted @ 2022-03-11 09:30  HanVeloce  阅读(411)  评论(0)    收藏  举报