zoukankan      html  css  js  c++  java
  • Java执行cmd命令

    Java JDK执行cmd命令的方法:

        //在单独的进程中执行指定的字符串命令。
        public Process exec(String command) throws IOException 
        
        //在指定环境的独立进程中执行指定命令和变量
        public Process exec(String command, String[] envp) throws IOException 
        
        //在单独的进程中执行指定命令和变量
        public Process exec(String cmdarray[]) throws IOException 
        
        //在指定环境的独立进程中执行指定的命令和变量
        public Process exec(String[] cmdarray, String[] envp) throws IOException 
    
        //在有指定环境和工作目录的独立进程中执行指定的字符串命令
        public Process exec(String command, String[] envp, File dir) throws IOException 
        
        //在指定环境和工作目录的独立进程中执行指定的命令和变量
        public Process exec(String[] cmdarray, String[] envp, File dir)

    代码:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    public class CMDTest {
        
        public static void main(String[] args) {
            
            // Java调用 dos命令
            String cmd = "ping www.baidu.com";
            try {
                Process process = Runtime.getRuntime().exec(cmd);
                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                
                String content = br.readLine();
                while (content != null) {
                    System.out.println(content);
                    content = br.readLine();
                }
                //杀掉进程
                process.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    
    }

     

    推荐阅读:https://www.cnblogs.com/tohxyblog/p/6501396.html

  • 相关阅读:
    抽象与接口的综合练习
    java构造函数能否被继承,为什么?
    题解 【USACO 4.2.1】草地排水
    题解 【NOI2010】超级钢琴
    题解 [USACO Mar08] 奶牛跑步
    题解 【NOIP2016】魔法阵
    题解 对称二叉树
    题解 【NOIP2014】解方程
    题解 【NOIP2010】关押罪犯
    题解 贪吃蛇
  • 原文地址:https://www.cnblogs.com/mxh-java/p/12922443.html
Copyright © 2011-2022 走看看