Java method to invoke DOS commands for Windows _java

Source: Internet
Author: User

This is an instance of calling a DOS command using Java code, where I'm not going to say, directly on the code, the code is as follows:

Copy Code code as follows:

Import java.io.*;
/**
* Java invoke DOS command for Windows
* Implement the ipconfig command that invokes windows, and then output the output to the console via IO flow.
*/
public class runwindowscommand{
public static void Main (string[] args) {
InputStream ins = null;
string[] cmd = new string[] {"cmd.exe", "/C", "ipconfig"}; Command
try {
Process process = Runtime.getruntime (). exec (CMD);
INS = Process.getinputstream (); Get information after executing the cmd command
BufferedReader reader = new BufferedReader (new InputStreamReader (INS));
String line = null;
while (line = Reader.readline ())!= null) {
System.out.println (line); Output
}
int exitvalue = Process.waitfor ();
System.out.println ("return value:" + exitvalue);
Process.getoutputstream (). Close (); Don't forget, you must shut it down.
catch (Exception e) {
E.printstacktrace ();
}
}
}

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.