All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wu.framework.easy.temple.util.CMD Maven / Gradle / Ivy

The newest version!
package com.wu.framework.easy.temple.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author : Jia wei Wu
 * @version 1.0
 * describe :
 * @date : 2020/12/1 下午10:12
 */
public class CMD {

    public static String executeLinuxCmd(String cmd) {
        System.out.println("执行命令[" + cmd + "]");
        Runtime run = Runtime.getRuntime();
        try {
            Process process = run.exec(cmd);
            String line;
            BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer out = new StringBuffer();
            while ((line = stdoutReader.readLine()) != null) {
                out.append(line);
            }
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            process.destroy();
            return out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(executeLinuxCmd("pwd"));
        ;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy