Java执行外部程序
2011-07-24 11:30
148 查看
Java执行外部程序其实是一个简单的操作
首先取得运行环境(JVM)
然后直接执行程序命令就可以
下面是相关的JDK官方注解
java.lang.Process
The
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (
The subprocess is not killed when there are no more references to the
There is no requirement that a process represented by a
Process java.lang.Runtime.exec(String command) throws IOException
Executes the specified string command in a separate process.
This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation
Parameters:command a specified system command.Returns:A new
首先取得运行环境(JVM)
然后直接执行程序命令就可以
try { Process proc = Runtime.getRuntime().exec("ipconfig"); //如果要取得执行命令的输出,那么可以通过proc.getInputStream()取得 DataInputStream in = new DataInputStream(proc.getInputStream()); Charset ch = Charset.forName("gb2312"); BufferedReader reader = new BufferedReader(new InputStreamReader(in, ch)); String str = reader.readLine(); while (null != str) { System.out.println(str); str = reader.readLine(); } } catch (IOException e) { e.printStackTrace(); }
下面是相关的JDK官方注解
java.lang.Process
The
ProcessBuilder.start()and
Runtime.execmethods create a native process and return an instance of a subclass of
Processthat can be used to control the process and obtain information about it. The class
Processprovides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (
getOutputStream(),
getInputStream(),
getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
The subprocess is not killed when there are no more references to the
Processobject, but rather the subprocess continues executing asynchronously.
There is no requirement that a process represented by a
Processobject execute asynchronously or concurrently with respect to the Java process that owns the
Processobject.
Process java.lang.Runtime.exec(String command) throws IOException
Executes the specified string command in a separate process.
This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation
exec(command, null, null).
Parameters:command a specified system command.Returns:A new
Processobject for managing the subprocessThrows:SecurityException - If a security manager exists and its
checkExecmethod doesn't allow creation of the subprocessIOException - If an I/O error occursNullPointerException - If
commandis
nullIllegalArgumentException - If
commandis emptySee Also:exec(String[], String[], File)ProcessBuilder
相关文章推荐
- 在Java中执行脚本命令以及调用外部程序说明
- java执行外部程序
- java调用需要输入的外部可执行程序
- Java执行外部程序,并等待完成
- Java执行外部程序的问题
- Java执行外部程序的问题
- JAVA基础应用——执行外部程序(Apache Commons Exec)
- java命令行执行程序解决依赖外部jar包的问题
- 将Java程序打包成可执行文件jar包,然后执行jar包,引用外部包的情况
- java调用外部程序执行
- [Java语言] Java执行外部程序并返回结果
- Bat文件执行java命令,报:javac不是内部或外部命令,也不是可运行的程序或批处理文件
- Centos命令行执行Java程序(引入外部Jar)
- Java 进程执行外部程序造成阻塞的一种原因
- [java调用外部可执行程序] 1. java使用cmd命令(转)
- 兼容timeout的Java执行外部命令的小程序
- Java Process Class : Java 执行外部程序及命令
- 在JAVA中调用外部可执行程序
- Java Process Class : Java 执行外部程序及命令
- Java执行外部程序(Apache Commons Exec)