Java Execute Linux command

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.