Execute cmd command in Java

Source: Internet
Author: User

One, Java three ways to execute the cmd command: http://www.jb51.net/article/80829.htm

Reference: https://www.cnblogs.com/zhufu9426/p/7928570.html,http://blog.csdn.net/Roy_70/article/details/51505314

Second, CMD command reference: https://baike.baidu.com/item/CMD%E5%91%BD%E4%BB%A4/9684689?fr=aladdin#3

Way One:

 Public Staticstring Checkphysicaladdress () {string physicaladdress= "Read MAC error!"; Try{String line; Process Process= Runtime.getruntime (). EXEC ("cmd/c ipconfig/all"); BufferedReader BufferedReader=NewBufferedReader (NewInputStreamReader (Process.getinputstream (), "GBK"));//solving output garbled problems with GBK encoding    while(line = Bufferedreader.readline ())! =NULL) {    if(Line.indexof ("Physical Address ...:")! =-1) {     if(Line.indexof (":")! =-1) {physicaladdress= Line.substring (Line.indexof (":") + 2);  Break;//Find your Mac, exit the Loop}}} process.waitfor (); } Catch(Exception e) {e.printstacktrace (); }  returnphysicaladdress;}

Way two:

 Public Static voidStartProgram (String Programpath)throwsIOException {log.info ("Launch application:" +Programpath); if(Stringutils.isnotblank (Programpath)) {Try{String ProgramName= Programpath.substring (Programpath.lastindexof ("/") + 1, Programpath.lastindexof ("."))); List<String> list =NewArraylist<string>(); List.add ("Cmd.exe"); List.add ("/C"); List.add ("Start"); List.add ("\" "+ ProgramName +" \ ""); List.add ("\" "+ Programpath +" \ ""); Processbuilder PBuilder=NewProcessbuilder (list);   Pbuilder.start (); } Catch(Exception e) {e.printstacktrace (); Log.error ("Application:" + Programpath + "does not exist! "); }  } } 

Way three:

 Public Static void throws IOException {  log.info ("Launch application:" + Programpath);   if (Stringutils.isnotblank (Programpath)) {   try  {    desktop.getdesktop (). Open (  New  File (Programpath));    Catch (Exception e) {    e.printstacktrace ();    Log.error ("Application:" + Programpath + "does not exist! ");   }  } }

Third, run external programs in Java using Runtime and process classes

Iv. waitfor () issues in process

Reference: The JDK help document says: If necessary, wait until the process represented by this process object has terminated. If the child process has been terminated, this method returns immediately. But calling this method directly causes the current thread to block until the child process exits. This is also explained on this JDK document: Because the local system is valid for the buffer pool provided by the standard input and output, it is possible for the error to write fast to the standard output and quickly read from the standard input, which can cause the child process to be or even deadlock. OK, the key to the problem is in the buffer place: the standard output of the executable program is more, and the standard buffer running the window is not large enough to block. And then we analyze the buffer, where is this thing, when the runtime object calls exec (cmd), the JVM initiates a child process that establishes three pipe connections to the JVM process: standard input, standard output, and standard error stream. Assuming that the program continues to write data to the standard output stream and the standard error stream, and the JVM does not read it, it will not be able to continue writing the data when the buffer is full, resulting in blocking at waitfor (). Knowing where the problem lies, we can solve the problem. The most way to view the online method is to open two threads before the WAITFOR () command reads the contents of the standard output buffer and standard error stream of the window.

    1. p = runtime.getruntime (). EXEC ("notepad.exe");
    2. P.waitfor ();

Summary: In the method exec ("notepad.exe"); The other program called does not Terminate, P.waitfor () indicates waiting here, does not continue execution of the following code, similar to the callback function

Execute cmd command in Java

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.