Java Execute Linux command

Source: Internet
Author: User

1  Public classStreamgobblerextendsThread {2       3 InputStream is; 4 String type; 5   6      PublicStreamgobbler (InputStream is, String type) {7          This. is =is ; 8          This. Type =type; 9     }  Ten    One      Public voidrun () { A         Try {   -InputStreamReader ISR =NewInputStreamReader (IS);  -BufferedReader br =NewBufferedReader (ISR);  theString line =NULL;  -              while(line = Br.readline ())! =NULL) {   -                 if(Type.equals ("Error")) {   -System.out.println ("Error:" +Line );  +}Else {   -System.out.println ("Debug:" +Line );  +                 }   A             }   at}Catch(IOException IoE) { - Ioe.printstacktrace ();  -         }   -     }   -}

1 Private voidShell (String cmd)2     {3String[] Cmds = {"/bin/sh", "-C", cmd};4 process process;5 6         Try7         {8Process =runtime.getruntime (). exec (CMDS);9 TenStreamgobbler Errorgobbler =NewStreamgobbler (Process.geterrorstream (), "Error"); OneStreamgobbler Outputgobbler =NewStreamgobbler (Process.getinputstream (), "Output"); A Errorgobbler.start (); - Outputgobbler.start (); -             Try the             { - process.waitfor (); -             } -             Catch(interruptedexception e) +             { - e.printstacktrace (); +             } A         } at         Catch(IOException e) -         { - e.printstacktrace (); -         } -}

The parameter cmd is the Linux command. Only one command can be executed at a time.

    1. Java runtime.exec () Considerations
    • Always read the data stream before calling the WAITFOR () method
    • Always read from the standard error stream before reading the standard output stream

2. The best way to execute a system command is to write a bat file or a shell script.

Resources:

Note reference:

http://blog.csdn.net/flying881114/article/details/6272472

Http://blog.csdn.net/westwin/archive/2005/04/22/358377.aspx

Http://blog.csdn.net/moreorless/archive/2009/05/15/4182883.aspx

Shell Script Reference:

Http://blog.csdn.net/HEYUTAO007/archive/2010/06/30/5705499.aspx

Java Execute Linux 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.