이 글에서는 "Java.Utils:"를 사용하여 명령줄 명령을 실행하는 방법을 소개합니다. 도움이 필요한 친구들이 모두 참고할 수 있기를 바랍니다.

package org.bood.common.utils;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Properties;/**
* <p>
* 执行命令行命令
* </p>
*
* @author:bood
* @date:2020/10/16
*/public class CommandUtils {
public CommandUtils() {
}
/**
* <p>
* 执行命令
* </p>
*
* @param commandLine: 命令
* @return:java.lang.String
* @author:bood
* @date:2020/10/16
*/
public static String execute(String commandLine) throws Exception {
String[] cmd = new String[3];
Properties props = System.getProperties();
String osName = props.getProperty("os.name").toLowerCase();
String charset=null;
String result="";
if (osName.startsWith("windows")) {
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[2] = commandLine;
charset="GBK";
} else if (osName.startsWith("linux")) {
cmd[0] = "sh";
charset="UTF-8";
}
Process ps = Runtime.getRuntime().exec(cmd);
String line = null;
BufferedReader input = new BufferedReader(new InputStreamReader(ps.getInputStream(),charset));
while ((line = input.readLine()) != null) {
result+=line+"\n";
}
input.close();
ps.destroy();
return result;
}}추천: "java 비디오 튜토리얼"
위 내용은 Java.Utils: 명령줄 명령을 실행하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!