ImportJava.io.*; Public classt{ Public Static voidMain (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 ("Delete connection information after enabling failed or delete WiFi:" +cmd);Process p =runtime.getruntime (). exec (CMD); BufferedReader BR=NewBufferedReader (NewInputStreamReader (P.getinputstream ())); String Line=NULL; //StringBuilder result = new StringBuilder (); while(line = Br.readline ())! =NULL) {System.out.println (line); } } Catch(Exception e) {e.printstacktrace (); } }}
Questions :
When executing cmd1, it is always impossible to execute on a Linux server, but when CMD2 is executed, it can be performed normally on a Linux server. If there is no quotation mark, there is a problem when there are special characters in the argument, there is a failure of execution or the parameter is different from the original parameter.
There is no better workaround for the time being, instead the command is executed using the existing incoming array method.
Java Execute Linux command