Java executes cmd command and gets output result

Source: Internet
Author: User

1.java execute cmd command and get output result

1 ImportJava.io.BufferedReader;2 ImportJava.io.InputStreamReader;3 4 ImportOrg.apache.commons.lang3.text.StrBuilder;5 6 /**7  *8  * @authorUser19  */Ten  Public classDeleteprogram { One      Public Static voidrun () { ARuntime runtime =runtime.getruntime (); -         Try { -BufferedReader br =NewBufferedReader (NewInputStreamReader (runtime.exec ("ipconfig"). getInputStream ())); the             //StringBuffer B = new StringBuffer (); -String line=NULL; -StringBuffer b=NewStringBuffer (); -              while((Line=br.readline ())! =NULL) { +B.append (line+ "\ n"); -             } + System.out.println (b.tostring ()); A}Catch(Exception e) { at e.printstacktrace (); -         } -  -     } -  -      Public Static voidMain (string[] args) { inDeleteprogram Delp =NewDeleteprogram (); - Delp.run (); to     } +  -}

2. Get the fields you need

 Public Staticstring Getbackage (string path) {Process p; //the command in Test.bat is Ipconfig/allString cmd = "aapt dump badging" +path;//String cmd= "Jarsigner-verify-verbose-certs c:\\users\\administrator\\desktop\\pandaclient.apk"; String ResultStr =NULL; Try          {              //Execute Commandp =runtime.getruntime (). exec (CMD); //the output stream that obtains the command resultInputStream fis=P.getinputstream (); //read with a read output stream class//read rows with buffersBufferedReader br=NewBufferedReader (NewInputStreamReader (FIS, "GB2312")); String Line=NULL; //until you're done reading .            inti = 0;  while((Line=br.readline ())! =NULL)              {               if(Line.contains ("package:")) {//parse the content that meets your needs, and then return directly. ResultStr=Line ;  Break; }//Resultstr=line;i++; }          }          Catch(IOException e) {e.printstacktrace (); }          returnResultStr;}
1      Public Static void Main (string[] args) {2         String url= "www.taobao.com"; 3         String response=null; 4         response=getsingle (URL); 5         6         System.out.println (response); 7     }

Java executes cmd command and gets output result

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.