JAVA执行cmd命令方法
package com.cmd; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class Test { /** * 调用方法 * @param cmd cmd命令 * @return */ public static String exec(String cmd) { try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd); InputStream es = proc.getErrorStream(); String line; BufferedReader br; br = new BufferedReader(new InputStreamReader(es, "GBK")); StringBuffer buffer=new StringBuffer(); while ((line = br.readLine()) != null) { buffer.append(line+"\n"); } return buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { System.out.println(exec("java -version")); } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)