Comparison between Java and C # Running command line examples

Source: Internet
Author: User

Haha, I just solved the example of running the command line in C # For the customer, and sorted out the Java example by the way. Let's take a look at the comparison.

 

Java

Import Java. io. bufferedreader; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/>/** <br/> * example of running command line in Java <br/> * @ author Java century network (java2000.net) <br/> */<br/> public class testprocess {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> // if you need to start the CMD window, use <br/> // CMD/K start to ping 127.0.0.1-T <br/> PROCESS p = runtime.getruntime(cmd.exe C ("Ping 127.0.0. 1-T "); <br/> inputstream is = P. getinputstream (); <br/> bufferedreader reader = new bufferedreader (New inputstreamreader (is); <br/> string line; <br/> while (line = reader. readline ())! = NULL) {<br/> system. out. println (line); <br/>}< br/> P. waitfor (); <br/> is. close (); <br/> reader. close (); <br/> P. destroy (); <br/>}catch (exception ex) {<br/> ex. printstacktrace (); <br/>}< br/>}

 

 

C #

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. diagnostics; <br/> using system. io; <br/>/** <br/> * C # example of running command line <br/> * @ author Java century network (java2000.net) <br/> */<br/> namespace consoleapplication1 <br/> {<br/> class testprocess <br/>{< br/> Public static void executecommand () <br/>{< br/> processstartinfo start = new processstartinfo ("ping.exe "); // Set the command line file for running the ping.exe file. The file system will find it by itself <br/> // if it is a temporary exefile, you can refer to it for details, such as running winrar.exe <br/> Start. arguments = "127.0.0.1-T"; // set the command parameter <br/> Start. createnowindow = true; // do not display the doscommand line window <br/> Start. redirectstandardoutput = true; // <br/> Start. redirectstandardinput = true; // <br/> Start. useshellexecute = false; // whether to specify the Operating System Shell Process Startup Program <br/> PROCESS p = process. start (start); <br/> streamreader reader = P. standar Doutput; // capture the output stream <br/> string line = reader. Readline (); // read a row each time <br/> while (! Reader. endofstream) <br/>{< br/> console. out. writeline (line); <br/> line = reader. readline (); <br/>}< br/> P. waitforexit (); // wait for the program to exit after execution <br/> P. close (); // close the Process <br/> reader. close (); // close the stream <br/>}< br/>

 

The running results are the same. Let's take a look.

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.