Linux commands in Java programs rely primarily on 2 classes: Process and runtime
First look at the process class: [plain] view plain copy print? The Processbuilder.start () and Runtime.exec methods create a native process and return an instance of the process subclass that can be used to control processes and obtain relevant information. The Process class provides methods for performing input from processes, performing output to processes, waiting for process completion, checking the exit status of a process, and destroying (killing) processes. The method of creating a process may not work well for specific processes on certain native platforms, such as native window processes, daemons, win16/dos processes on Microsoft Windows, or shell scripts. The child process created does not have its own terminal or console. All of its standard IO (i.e. stdin, stdout, and STDERR) operations are redirected to the parent process through three streams (Getoutputstream (), getInputStream (), and Geterrorstream ()). The parent process uses these streams to provide input to the subprocess and to obtain output from the child process. Because some native platforms provide only a limited buffer size for standard input and output streams, if the output stream or input stream of a read-write subprocess fails quickly, it can cause a child process to block or even create a deadlock. When there are no more references to the process object, instead of deleting the subprocess, the child process continues to execute asynchronously. For a Java process with a process object, it is not necessary to asynchronously or concurrently execute the process represented by a process object.
The Processbuilder.start () and Runtime.exec methods create a native process and return an instance of the process subclass that can be
used to control processes and obtain relevant information. The Process class provides methods for performing input from processes, performing output to processes, waiting for process completion,
checking the exit status of a process, and destroying (killing) processes. The
method of creating a process may not work well for specific processes on certain native platforms, such as native window processes, daemons,
win16/dos processes on Microsoft Windows, or shell scripts. The child process created does not have its own terminal or console. All of its standard IO (i.e. stdin, stdout, and stderr)
operations are redirected to the parent process through three streams (Getoutputstream (), getInputStream (), and Geterrorstream ()).
The parent process uses these streams to provide input to the subprocess and to obtain output from the child process. Because some native platforms provide only a limited buffer size for standard input and output streams,
if the output stream or input stream of a read-write subprocess fails quickly, it can cause a child process to block or even create a deadlock.
when there are no more references to the process object, instead of deleting the subprocess, the child process continues to execute asynchronously.
for a Java process with a process object, it is not necessary to asynchronously or concurrently execute the process represented by a process object.
Special attention should be paid to:
1, the child process created does not have its own terminal console, and all callout operations will pass through three streams
(Getoutputstream (), getInputStream (), and Geterrorstream ()) are redirected to the parent process (the parent process can determine the execution of the child process through these flows)
2, because some native platforms provide only a limited buffer size for standard input and output streams, and if the output stream or input stream of a read-write subprocess fails quickly,
May cause a child process to block or even create a deadlock[Plain] View Plain copy print? Abstract void destroy () kill the child process. abstract int exitvalue () returns the export value of the child process. By convention, a value of 0 indicates a normal termination. Abstract inputstream geterrorstream () gets the error stream for the child process. Abstract inputstream getinputstream () gets the input stream for the child process. Abstract outputstream getoutputstream () gets the output stream of the child process. abstract int waitfor () causes the current thread to wait and, if necessary, to wait until the process represented by the Process object has terminated. &NBsp; This method returns immediately if the child process has been terminated. If the child process is not terminated, the invoked thread is blocked until the child process is exited.
Abstract void Destroy ()
kills the child process. the abstract int exitvalue ()
returns the export value of the subprocess. By convention, a value of 0 indicates a normal termination.
Abstract InputStream Geterrorstream ()
gets the error stream for the child process.
Abstract InputStream getInputStream ()
gets the input stream of the child process.
Abstract OutputStream Getoutputstream ()
gets the output stream of the child process. The
abstract int waitFor ()
causes the current thread to wait and, if necessary, to wait until the process represented by that process object has terminated.
If the child process has been terminated, this method returns immediately. If the child process is not terminated, the invoked thread is blocked until the child process is exited.
In particular, it is important to note that if the input stream in the subprocess is more than the content in the output stream or error stream, it is best to use caching (note above 2)
Let's take a look at the runtime class: [plain] view plain copy print? Each Java application has a runtime class instance that enables the application to connect to the environment in which it is running. The current run-time environment can be obtained through the GetRuntime method. The application cannot create its own instance of the runtime class.
Each Java application has a runtime class instance that enables the application to connect to the environment in which it is running. The current run-time environment can be obtained through the GetRuntime method.
Introduce several main methods:[Plain] View Plain copy print? Process exec (String command) Executes the specified string command in a separate process. process exec (STRING COMMAND, STRING[] ENVP) executes the specified string command in a separate process in the specified environment. process exec (String command, string[] envp, file dir) executes the specified string command in a separate process with the specified environment and working directory. process exec (string[] cmdarray) executes the specified commands and variables in a separate process. process exec (STRING[] CMDARRAY, STRING[] ENVP) executes the specified command and variable in a separate process in the specified environment. process exec (String[] cmdarray, string[] envp, file dir) executes the specified commands and variables in a separate process for the specified environment and working directory.
Process exec (String command)
executes the specified string commands in a separate process.
process Exec (String command, string[] envp)
executes the specified string command in a separate process in the specified environment.
process Exec (String command, string[] envp, File dir)
executes the specified string command in a separate process with the specified environment and working directory.
process exec (string[] cmdarray)
executes the specified command and variable in a separate process.
process exec (string[] cmdarray, string[] ENVP
executes the specified command and variable in a separate process in the specified environment.
process exec (string[] cmdarray, string[] envp, File dir
executes the specified commands and variables in a separate process for the specified environment and working directory.
Command: A specified system command. ENVP: An array of environment variable strings in which each environment variable is formatted as Name=value, or null if the child process should inherit the environment of the current process. Dir: The working directory of the child process, or null if the child process should inherit the working directory of the current process. Cmdarray: An array that contains the commands that are invoked and their arguments.
The following is an example (to play an executable jar package and throw it to Linux for execution):[Java] View Plain copy print? public class test { public static void main ( String[] args) { inputstream in = null; try { process pro = runtime.getruntime (). EXEC (new string[]{"sh", "/home/test/test.sh", "select admin from m_admin ", "/home/test/result.txt"}); pro.waitfor (); in = pro.getinputstream (); bufferedreader read = new bufferedreader (new InputStreamReader (in)); String result = read.readline (); system.out.println ("INFO:" +result); } catch (exception e) { e.printstacktrace (); } } }
public class Test {public
static void Main (string[] args) {
inputstream in = null;
try {
Process pro = Runtime.getruntime (). EXEC (new string[]{"sh",
"/home/test/test.sh", "select admin from m_ ADMIN ",
"/home/test/result.txt "});
Pro.waitfor ();
in = Pro.getinputstream ();
BufferedReader read = new BufferedReader (new InputStreamReader (in));
String result = Read.readline ();
System.out.println ("INFO:" +result);
} catch (Exception e) {
e.printstacktrace ();}}}
This method is used in the process exec (string[] cmdarray)
The
/home/test/test.sh script is as follows: [plain] view plain copy print? #!/bin/sh # Query sql sql=$1 #查询结果保存文件 result_file=$2 #数据库连接 db_name= scott db_pwd=tiger db_server=db_test result= ' Sqlplus -S ${DB_NAME}/${DB_PWD}@${DB_SERVER}<< ! set heading off set echo off set pages 0 set feed off set linesize 3000 ${sql} / commit / ! ' echo "${result}" >> ${RESULT_FILE} Echo 0;