标签:class delete put 连接 执行命令 out cmd cli lin
import java.io.*; public class T{ public static void main(String[] args) { // String cmd1 = "sudo nmcli connection delete ‘"+args[0]+"‘"; // String cmd2 = "sudo nmcli connection delete "+args[0]+""; String[] cmd = {"sh","-c","sudo nmcli con delete ‘"+args[0]+"‘"}; System.out.println(cmd); try { // logger.info("启用失败或删除wifi后删除连接信息:"+cmd); Process p = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; // StringBuilder result = new StringBuilder(); while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } }
问题:
执行cmd1时,始终无法在linux服务器执行,但若执行cmd2时,就可以正常在linux服务器执行。若不加引号时,参数存在特殊字符时会存在问题,出现执行失败或参数与原本传入参数不同的问题。
暂时没有发现更好的解决办法,改为使用现有传入数组的方式,执行命令。
标签:class delete put 连接 执行命令 out cmd cli lin
原文地址:http://www.cnblogs.com/javadongx/p/java_linux_cmd.html