Java Implementation Operation DOS command

Source: Internet
Author: User

Java implements two ways to operate DOS commands

1. Read the commands in the file

Package Com;import Java.io.inputstream;public class cmd {public        static void Main (string[] args) {        String path = " d:\\cmd.bat";        Runtime run = Runtime.getruntime ();        try {            //run.exec ("cmd/k shutdown-s-T 3600");            Process process = Run.exec ("cmd.exe/k start" + path);            InputStream in = Process.getinputstream ();            while (In.read ()! =-1) {                System.out.println (In.read ());            }            In.close ();            Process.waitfor ();        } catch (Exception e) {            e.printstacktrace ();}}    }

The results of the operation are as follows:

Second way, Direct read command

Package Com;import Java.io.BufferedReader;   Import Java.io.InputStreamReader;  Public classQUMF { Public Static voidMain (string[] args) {Try{Runtime rt=Runtime.getruntime (); Process PR= Rt.exec ("cmd/c Ping www.baidu.com && dir "); //Process PR = rt.exec ("d:\\xunlei\\project.aspx"); BufferedReader input =NewBufferedReader (NewInputStreamReader (Pr.getinputstream (),"GBK")); String Line=NULL;  while(line = Input.readline ())! =NULL) {System. out. println (line); }                intExitval =pr.waitfor (); System. out. println ("Exited with error code"+exitval); } Catch(Exception e) {System. out. println (E.tostring ());          E.printstacktrace (); }      }  }  

Java Implementation Operation DOS 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.