Calling the DOS shell command in a Java program--here, for example, invoking the doc command

Source: Internet
Author: User

/** *  java calling DOS command for Windows  */public class RunDocInJava{     Public static void main (String[] args)  {         inputstream ins = null;        string[] cmd  = new String[] {  "cmd.exe",  "/C",  "ipconfig"  };  //  Command line         try {        / /runtime.exec () used to execute external programs or commands            Process  Process = runtime.getruntime (). EXEC (cmd);            ins = process.getinputstream ();  //  get information after executing the doc command              //gets the information into the buffer as much as possible              bUfferedreader reader = new bufferedreader (New inputstreamreader (INS));                String line =  null;              //Reading Information              //will read the buffer information first, if the information is not fully read the source data              while  ((Line = reader.readline ())  !=  null)  {                    system.out.println (line);              }            int exitval  = process.waitfor ();  //Get program return value     success is "0"             &nbsP System.out.println ("return integer:  " +exitval);             process.getoutputstream (). Close (),   //if not closed in the program will cause a stack overflow          } catch  (exception e)  {             e.printstacktrace ();         }     }}


Perform:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/76/26/wKioL1ZMBuXRsohXAAI5aleHLFA058.jpg "title=" ~wn) Iahn5$ob2osbk~x%7t3.jpg "alt=" Wkiol1zmbuxrsohxaai5alehlfa058.jpg "/>

Some examples of the differences between Windows and Linux are called:

Windows downgrade program

Process proc =Runtime. GetRuntime (). exec ("exefile") ;

The Linux downgrade program will be changed to the following format

Process proc =Runtime. GetRuntime (). exec ("./exefile") ;

Windows downgrade system commands

String [] cmd={"cmd","/C","copy exe1 Exe2"};
Process proc =Runtime.GetRuntime().exec(cmd);

The Linux downgrade System command will be changed to the following format

String [] cmd={"/bin/sh","- C","ln-s exe1 Exe2"};
Process proc =Runtime.GetRuntime().exec(cmd);

Windows downgrades system commands and pops up a command-line window

String [] cmd={"cmd","/C","start copy exe1 exe2"};
Process proc =Runtime.GetRuntime().exec(cmd);

Linux downgrade system command and pop-up terminal window will be changed to the following format

String [] cmd={"/bin/sh","- C","xterm-e ln-s exe1 exe2"};
Process proc =Runtime.GetRuntime().exec(cmd);

and the working directory to set the calling program.

Process proc =Runtime.GetRuntime().exec("Exeflie",NULL, New File("Workpath"));

Examples of Windows and Linux call differences are referenced from: http://robinjoe.iteye.com/blog/1201388

This article is from "The rookie in the struggle ..." Blog, be sure to keep this source http://2015arong.blog.51cto.com/8994290/1713963

Calling the DOS shell command in a Java program--here, for example, invoking the doc command

Related Article

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.