有时,我们需要在java程序中调用外部程序,我们可用通过Runtime.exec()调用来完成。
The class java.lang.Runtime
features a static method called getRuntime()
, which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime
object. With that reference, you can run external programs by invoking the Runtime
class's exec()
method. Developers often call this method to launch a browser for displaying a help page in HTML.
exec()有四个重载版本
There are four overloaded versions of the exec()
command:
-
public Process exec(String command);
-
public Process exec(String [] cmdArray);
-
public Process exec(String command, String [] envp);
-
public Process exec(String [] cmdArray, String [] envp);
For each of these methods, a command -- and possibly a set of
arguments -- is passed to an operating-system-specific function call.
This subsequently creates an operating-system-specific process (a
running program) with a reference to a Process
class returned to the Java VM. The Process
class is an abstract class, because a specific subclass of Process
exists for each operating system.
You can pass three possible input parameters into these methods:
- A single string that represents both the program to execute and any arguments to that program
- An array of strings that separate the program from its arguments
- An array of environment variables
Pass in the environment variables in the form name=value
. If you use the version of exec()
with a single string for both the program and its arguments, note that
the string is parsed using white space as the delimiter via the StringTokenizer
class.
注意事项:
1. 当调用的外部命令中包含重定向(<、>),管道( | ) 命令时,exec(String command)的版本不能正确解析重定向、管道操作符。所以需要使用exec(String [] cmdArray)。
如, echo "hello world" > /home/admin/newFile.txt
ls -e | grep java
需要使用如下的调用方式
String []cmdArray = new String[]{ "/bin/sh", "-c", "ls -e | grep java"};
Runtime.getRuntime().exec(cmdArray);
2.
永远要在调用waitFor()方法之前读取数据流
永远要先从标准错误流中读取,然后再读取标准输出流
The next version of Savant is going to focus heavily on the
stand-alone runtime and support for dialects and plugins. Supporting
all that is largely handled by using a simple executor framework I
wrote around Java 1.4 and lower’s Runtime.exec method. A few things to
keep in mind when using this:
- Always read from the streams prior to calling waitFor. Otherwise
you could end up waiting forever on Windows and other OS platforms
whose I/O buffers can’t store enough from standard out and standard
error to ensure the program has finished. These platforms will pause
the execution of whatever is running until something reads the buffered
content from standard out and standard error. I would imagine all
platforms suffer from this, but some platforms have larger buffers than
others. Needless to say, always read from the streams first.
- Always read from standard error first. I ran across a bug where
some OS platforms will always open standard out, but never close it.
What this means is that if you read from standard out first and the
process only writes to standard error, you’ll hang forever waiting to
read. If you read from standard error first, you’ll always be okay on
these platforms because the OS seems to shutdown standard error. I
think however, that the best way to handle all cases is to check both
standard error and standard out for readiness and only read from them
if they have something to offer. The downside I could see here is that
error isn’t ready, but eventually will be.
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.
在exec()后 立即调用waitFor()会导致进程挂起。
相关文章:
1. When Runtime.exec() won't 关于Runtime的注意事项
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
分享到:
相关推荐
使用runtime, 实现java客户端多次调用linux命令或者shell脚本,并返回对应结果或信息
在java代码中调用执行shell脚本,sqlldr导数与使用sqlplus在shell调用执行存储过程。 linux环境中有2个dba的用户:oracle、erm 还有1个web用户:erm 在linux环境中,这三个用户都可以直接在任意目录下执行该shell...
第一次接触linux系统,之前写的数据导出不好使了。...找了好多资料,最后决定写个shell脚本;没接触过shell脚本,网上大部分例子都写的挺复杂的;贴个简单的,不带传参什么的;只是最简单的数据表的导出备份
利用Runtime.execute方法,我们可以在Java程序中运行Linux的Shell脚本,或者执行其他程序
完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定
windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...
Runtime 执行bat
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # -------------------------------------...
将runtime.exec()的四个陷阱用中文描述
java执行shell或bat脚本,Java可以使用三种方式来执行.bat或.shell脚本文件:使用Runtime.exec()、使用ProcessBuilder、使用第三方工具包commonsexec.jar
java 执行可执行文件,Runtime.exec、ProcessBuilder、commons-exec
NULL 博文链接:https://jisonami.iteye.com/blog/2181810
复制代码 代码如下:import java.io.BufferedReader;... Process pr = Runtime.getRuntime().exec(“python test.py”); BufferedReader in = new BufferedReader(new InputStreamReader(
Java调用Linux命令 调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例, (注意:Runtime.getRuntime().exec(command)返回的是一个Process类的实例), 该实例可用于控制进程或取得进程的...
Unity3D 运行时编辑器Runtime Editor 用于学习用 正版请去Unity Asset Store 下载
自己研究的runtime知识,对于理解ios的底层开发有些小帮助
onnx的java包,pom引入后可以试验java调用onnx文件进行cpu模型推理。 具体见https://github.com/microsoft/onnxruntime/blob/master/java/src/test/java/sample/ScoreMNIST.java 和 ...
Ubuntu 18.04下通过Matlab runtime使用c++调用自己编写的matlab函数 自己根据自己配置环境,所写的基础文件包 下面是自己配置过程博客:...
资源包含175张训练用数据,onnxruntime执行脚本。安装依赖后,可直接运行脚本,获取生成的结果。如若获取资源后无法执行,请私信。
1)启动性能监控脚本,cpu与memory信息记录于文件中。 ./watch_start runtime filePrefix runtime暂时为运行次数。根据runtime计算最佳统计间隔和次数的模块未实现。 记录文件名:filePrefix_timestamp_cpu.txt 或 ...