How Java calls the shell script

Source: Internet
Author: User
Tags readline

There are times when a feature is embedded in the Java function, which is implemented by a shell script. At this point, Java support for scripting calls is required.

Test environment

Ubuntu16.04 I3-6100,12GB

Hello World

Take a look at a basic example

    Process exec = Runtime.getRuntime().exec(new"uname" ,"-a"});    exec.waitFor();    BufferedReader reader =            new BufferedReader(new InputStreamReader(exec.getInputStream()));    System.out.println(reader.readLine());Linux jason-Inspiron-36504.4.0-121-generic #1451313:47:232018 x86_64 x86_64 x86_64 GNU/Linux
Interpreting process

The Java.lang.Process class provides a way to get input, output, wait for execution, and destroy processes.
The process class can create instances from Processbuilder.start () and runtime.exec, starting with Java1.5, Processbuilder.start () is a more recommended practice, But online tutorials are more recommended with the Runtime.exec () method.

Modifier and Type Method Description
abstract void destroy () kills the subprocess.
abstract int exitvalue () Returns The exit value for the subprocess.< /td>
abstract InputStream geterrorstream () Returns the input stream Connected to the error output of the subprocess.
abstract InputStream getinputstream () Returns the input stream Conne CTED to the normal output of the subprocess.
abstract OutputStream getoutputstream () Returns the output stream con Nected to the normal input of the subprocess.
abstract int waitFor () causes the current thread to wait, if Necessa Ry, until the process represented by this process object has terminated.

On the inheritance system, the implementation class for the process is built into the JDK, and the Linux version of the JDK comes with only one implementation class unixprocess.

Interacting with scripts

Process can not only execute processes, but also get the return results of the process.

    PrivateStringExecuteCommand(String command) {StringBuffer output =NewStringBuffer (); Process p;Try{p = Runtime.GetRuntime().exec(command);intExitCode = P.WaitFor(); System. out.println(ExitCode); BufferedReader reader =NewBufferedReader (NewInputStreamReader (p.getInputStream())); String line =""; while(line = reader.)ReadLine()) !=NULL) {output.Append(Line +"\ n"); }        }Catch(Exception e) {e.Printstacktrace(); } System. out.println(Output.toString());returnOutput.toString(); }ping www.a.Shifen.com(111.13.100.91) About( -) bytes of data. -Bytes fromlocalhost(111.13.100.91): icmp_seq=1Ttl= theTime=7.66Ms -Bytes fromlocalhost(111.13.100.91): icmp_seq=2Ttl= theTime=7.90Ms -Bytes fromlocalhost(111.13.100.91): icmp_seq=3Ttl= theTime=14.0Ms---www.a.Shifen.comPing Statistics---3Packets Transmitted,3Received0% packet loss, time 2003msrtt Min/avg/max/mdev =7.668/9.861/14.013/2.937Ms
Summarize

Java executes scripts in a way that is similar to executing scripts directly inside bash, except for some changes in the environment, and the effect of execution is basically consistent with bash.

This article was published in the public number: Cola small data (Xiaokele_data).

How Java calls the shell script

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.