Runtime.exec的一些事
来源:互联网 发布:linux cens输入法切换 编辑:程序博客网 时间:2024/05/21 17:22
Runtime.exec的一些事
0 预备知识
1 不正确的调用exitValue
2不正确的调用waitFor
3 一种可接受的调用方式
4 调用认为是可执行程序的时候容易发生的错误
5 window执行的良好示例
6 不良好的重定向命令输出
7 良好的重定向输出示例
8 总结
9 问答
0 预备知识
Runtime类是一个与JVM运行时环境有关的Singleton类,有以下几个值得注意的地方:
0.1Runtime.getRuntime()可以取得当前JVM的运行时环境,这也是在Java中唯一得到运行时环境的方法。
0.2 Runtime上其他大部分的方法都是实例方法,也就是说每次进行运行时调用时都要用到getRuntime方法。
0.3 Runtime中的exit方法是退出当前JVM的方法,估计也是唯一的。System类中的exit实际上也是通过调用Runtime.exit()来退出JVM的。
Java对Runtime返回值的一般规则,0代表正常退出,非0代表异常中止,这只是Java的规则,在各个操作系统中总会发生一些小的混淆。
0.4Runtime.addShutdownHook()方法可以注册一个hook在JVM执行shutdown的过程中,方法的参数只要是一个初始化过但是没有执行的Thread实例就可以。(注意,Java中的Thread都是执行过了就不值钱的哦)
0.5说到addShutdownHook这个方法就要说一下JVM运行环境是在什么情况下shutdown或者abort的。
Shutdown:当最后一个非精灵进程退出或者收到了一个用户中断信号、用户登出、系统shutdown、Runtime的exit方法被调用时JVM会启动shutdown的过程,在这个过程开始后,他会并行启动所有登记的shutdown hook(注意是并行启动,这就需要线程安全和防止死锁)。当shutdown过程启动后,只有通过调用halt方法才能中止shutdown的过程并退出JVM。
Abort: abort退出时JVM就是停止运行但并不一定进行shutdown。这只有JVM在遇到SIGKILL信号或者windows中止进程的信号、本地方法发生类似于访问非法地址一类的内部错误时会出现。这种情况下并不能保证shutdown hook是否被执行。
0.6 Runtime.exec()方法的所有重载。这里要注意的是:
public Processexec(String[] cmdarray, String[] envp, File dir);
这个方法中cmdArray是一个执行的命令和参数的字符串数组,数组的第一个元素是要执行的命令往后依次都是命令的参数,envp中是name=value形式的环境变量设置,如果子进程要继承当前进程的环境时是null。
1 不正确的调用exitValue
1. public class BadExecJavac {
2. public static void main(String args[]) {
3. try {
4. Runtime rt = Runtime.getRuntime();
5. Process proc = rt.exec("java");
6. int exitVal = proc.exitValue();
7. System.out.println("Process exitValue: " + exitVal);
8. } catch (Throwable t) {
9. t.printStackTrace();
10. }
11. }
12. }
输出
1. java.lang.IllegalThreadStateException: process has not exited
2. at java.lang.ProcessImpl.exitValue(Native Method)
3. at BadExecJavac.main(BadExecJavac.java:26)
错误分析:
主要问题就是错误的调用了exitValue来取得外部命令的返回值。因为exitValue方法是非阻塞的,在调用这个方法时外部命令并没有返回所以引起异常。阻塞形式的方法是waitFor,它会一直等待外部命令执行完毕,然后返回执行的结果。
当你在一个Process上调用waitFor方法时,当前线程是阻塞的,如果外部命令无法执行结束,那么你的线程就会一直阻塞下去,这种意外会影响我们程序的执行。所以在我们不能判断外部命令什么时候执行完毕而我们的程序还需要继续执行的情况下,我们就应该循环的使用exitValue来取得外部命令的返回状态,并在外部命令返回时作出相应的处理。
2不正确的调用waitFor
1. public class BadExecJavac2 {
2. public static void main(String args[]) {
3. try {
4. Runtime rt = Runtime.getRuntime();
5. Process proc = rt.exec("javac");
6. int exitVal = proc.waitFor();
7. System.out.println("Process exitValue: " + exitVal);
8. } catch (Throwable t) {
9. t.printStackTrace();
10. }
11. }
12.}
不幸的是,这个程序也无法执行完成,它没有输出但却一直悬在那里!这是为什么那?
JDK文档中的解释:因为本地的系统对标准输入和输出所提供的缓冲池有效,所以错误的对标准输出快速的写入和从标准输入快速的读入都有可能造成子进程的锁,甚至死锁。
JDK仅仅说明为什么问题会发生,却并没有说明这个问题怎么解决。
解决方法就是:执行完外部命令后我们要控制好Process的所有输入和输出(视情况而定),//在这个例子里边因为调用的是Javac,而他在没有参数的情况下会将提示信息输出到标准出错,所以在下面的程序中我们要对此进行处理。
3 一种可接受的调用方式
1. public class MediocreExecJavac {
2. public static void main(String args[]) {
3. try {
4. Runtime rt = Runtime.getRuntime();
5. Process proc = rt.exec("javac");
6. InputStream stderr = proc.getErrorStream();
7. InputStreamReader isr = new InputStreamReader(stderr);
8. BufferedReader br = new BufferedReader(isr);
9. String line = null;
10. System.out.println("<error></error>");
11. while ((line = br.readLine()) != null)
12. System.out.println(line);
13. System.out.println("");
14. int exitVal = proc.waitFor();
15. System.out.println("Process exitValue: " + exitVal);
16. } catch (Throwable t) {
17. t.printStackTrace();
18. }
19. }
20.}
输出:
1. <error></error>
2. Usage: javac <options></options> <source files=""></source>
3.
4. ...
5.
6. Process exitValue: 2
结果分析:
出来了结果。
为了处理好外部命令大量输出的情况,你要确保你的程序处理好外部命令所需要的输入或者输出。
4 调用认为是可执行程序的时候容易发生的错误
1. public class BadExecWinDir {
2. public static void main(String args[]) {
3. try {
4. Runtime rt = Runtime.getRuntime();
5. Process proc = rt.exec("dir");
6. InputStream stdin = proc.getInputStream();
7. InputStreamReader isr = new InputStreamReader(stdin);
8. BufferedReader br = new BufferedReader(isr);
9. String line = null;
10. System.out.println("<output></output>");
11. while ((line = br.readLine()) != null)
12. System.out.println(line);
13. System.out.println("");
14. int exitVal = proc.waitFor();
15. System.out.println("Process exitValue: " + exitVal);
16. } catch (Throwable t) {
17. t.printStackTrace();
18. }
19. }
20. }
输出:
1. java.io.IOException: Cannot run program "dir": CreateProcess error=2, ...
2. at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
3. at java.lang.Runtime.exec(Runtime.java:593)
4. at java.lang.Runtime.exec(Runtime.java:431)
5. at java.lang.Runtime.exec(Runtime.java:328)
6. at BadExecWinDir.main(BadExecWinDir.java:29)
原因分析:
因为dir命令是由windows中的解释器解释的,直接执行dir时无法找到dir.exe这个命令,所以会出现文件未找到这个2的错误。如果我们要执行这样的命令,就要先根据操作系统的不同执行不同的解释程序command.com 或者cmd.exe。
5window执行的良好示例
1. public class StreamGobbler extends Thread {
2. InputStream is;
3. String type;
4.
5. StreamGobbler(InputStream is, String type) {
6. this.is = is;
7. this.type = type;
8. }
9.
10. public void run() {
11. try {
12. InputStreamReader isr = new InputStreamReader(is);
13. BufferedReader br = new BufferedReader(isr);
14. String line = null;
15. while ((line = br.readLine()) != null)
16. System.out.println(type + ">" + line);
17. } catch (IOException ioe) {
18. ioe.printStackTrace();
19. }
20. }
21. }
1. public class GoodWindowsExec {
2. public static void main(String args[]) {
3. if (args.length < 1) {
4. System.out.println("USAGE: java GoodWindowsExec <cmd></cmd>");
5. System.exit(1);
6. }
7. try {
8. String osName = System.getProperty("os.name");
9. String[] cmd = new String[3];
10. if (osName.equals("Windows NT")) {
11. cmd[0] = "cmd.exe";
12. cmd[1] = "/C";
13. cmd[2] = args[0];
14. } else if (osName.equals("Windows 95")) {
15. cmd[0] = "command.com";
16. cmd[1] = "/C";
17. cmd[2] = args[0];
18. }
19. Runtime rt = Runtime.getRuntime();
20. System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
21. Process proc = rt.exec(cmd);
22. // any error message?
23. StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
24. // any output?
25. StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
26. // kick them off
27. errorGobbler.start();
28. outputGobbler.start();
29. // any error???
30. int exitVal = proc.waitFor();
31. System.out.println("ExitValue: " + exitVal);
32. } catch (Throwable t) {
33. t.printStackTrace();
34. }
35. }
36. }
输出:
ExitValue: 0
原因分析:
就是cmd.exe /C +一个windows中注册了后缀的文档名,windows会自动地调用相关的程序来打开这个文档。
不要假设你执行的程序是可执行的程序,要清楚自己的程序是单独可执行的还是被解释的, 这里还有一点,就是得到process的输出的方式是getInputStream,这是因为我们要从Java 程序的角度来看,外部程序的输出对于Java来说就是输入,反之亦然。
6 不良好的重定向命令输出
错误的认为exec方法会接受所有你在命令行或者Shell中输入并接受的字符串。这些错误主要出现在命令作为参数的情况下,程序员错误的将所有命令行中可以输入的参数命令加入到exec中。下面的例子中就是一个程序员想重定向一个命令的输出。
1. public class BadWinRedirect {
2. public static void main(String args[]) {
3. try {
4. Runtime rt = Runtime.getRuntime();
5. Process proc = rt.exec("java jecho 'Hello World' > test.txt");
6. // any error message?
7. StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
8. // any output?
9. StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
10. // kick them off
11. errorGobbler.start();
12. outputGobbler.start();
13. // any error???
14. int exitVal = proc.waitFor();
15. System.out.println("ExitValue: " + exitVal);
16. } catch (Throwable t) {
17. t.printStackTrace();
18. }
19. }
20. }
程序员的本意是将Hello World这个输入重订向到一个文本文件中,但是这个文件并没有生成,jecho仅仅是将命令行中的参数输出到标准输出中,用户觉得可以像dos中重定向一样将输出重定向到一个文件中,但这并不能实现,用户错误的将exec认为是一个shell解释器,但它并不是,如果你想将一个程序的输出重定向到其他的程序中,你必须用程序来实现他。可用java.io中的包。
7 良好的重定向输出示例
1. public class StreamGobbler extends Thread {
2. InputStream is;
3. String type;
4. OutputStream os;
5. StreamGobbler(InputStream is, String type) {
6. this(is, type, null);
7. }
8. StreamGobbler(InputStream is, String type, OutputStream redirect) {
9. this.is = is;
10. this.type = type;
11. this.os = redirect;
12. }
13. public void run() {
14. try {
15. PrintWriter pw = null;
16. if (os != null)
17. pw = new PrintWriter(os);
18. InputStreamReader isr = new InputStreamReader(is);
19. BufferedReader br = new BufferedReader(isr);
20. String line = null;
21. while ((line = br.readLine()) != null) {
22. if (pw != null)
23. pw.println(line);
24. System.out.println(type + ">" + line);
25. }
26. if (pw != null)
27. pw.flush();
28. } catch (IOException ioe) {
29. ioe.printStackTrace();
30. }
31. }
32. }
1. public class GoodWinRedirect {
2. public static void main(String args[]) {
3. args = new String[1];
4. args[0]="g:\\out.txt";
5. if (args.length < 1) {
6. System.out.println("USAGE java GoodWinRedirect <outputfile></outputfile>");
7. System.exit(1);
8. }
9. try {
10. FileOutputStream fos = new FileOutputStream(args[0]);
11. Runtime rt = Runtime.getRuntime();
12. Process proc = rt.exec("java jecho 'Hello World'");
13. // any error message?
14. StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
15. // any output?
16. StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", fos);
17. // kick them off
18. errorGobbler.start();
19. outputGobbler.start();
20. // any error???
21. int exitVal = proc.waitFor();
22. System.out.println("ExitValue: " + exitVal);
23. fos.flush();
24. fos.close();
25. } catch (Throwable t) {
26. t.printStackTrace();
27. }
28. }
29. }
8 总结
总结了几条规则,防止我们在进行Runtime.exec()调用时出现错误。
· 在一个外部进程执行完之前你不能得到他的退出状态
· 在你的外部程序开始执行的时候你必须马上控制输入、输出、出错这些流。
· 你必须用Runtime.exec()去执行程序
· 你不能象命令行一样使用Runtime.exec()。
9 问答
问:为什么Runtime.exec("ls")没有任何输出?
答:调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例,该实例可用于控制进程或取得进程的相关信息。
由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO(如stdin,stdou,stderr)都通过Process.getOutputStream(),Process.getInputStream(),Process.getErrorStream()方法重定向给它的父进程了。用户需要用这些stream来向子进程输入数据或获取子进程的输出。所以正确执行Runtime.exec("ls")的例程如下:
1. try {
2. Process process = Runtime.getRuntime().exec(command);
3. InputStreamReader ir = new InputStreamReader(process.getInputStream());
4. LineNumberReader input = new LineNumberReader(ir);
5. String line;
6. while ((line = input.readLine()) != null)
7. System.out.println(line);
8. } catch (java.io.IOException e) {
9. System.err.println("IOException " + e.getMessage());
10. }
- Runtime.exec的一些事
- Java中Runtime.exec的一些事
- Java中Runtime.exec的一些事
- 调用Runtime.exec()的一些陷阱
- 调用Runtime.exec()的一些陷阱
- Runtime的exec方法
- Runtime.exec()的使用
- Runtime.exec()的使用
- Runtime.exec() 的陷阱
- Runtime.exec()的使用
- Runtime.exec()的使用
- Runtime.exec() 的陷阱
- java的Runtime的exec
- (转贴)Runtime.exec()的陷阱
- java runtime.exec() 的编写
- Runtime.exec()的使用 (zz)
- 翻译的文章Runtime.exec()
- java runtime.exec() 的编写
- Android统计图形绘制及点击事等功能的实现-achartengine篇
- 合理使用正则表达式解析hmtl
- 经典SQL语句大全
- Ubuntu 12.04安装英汉词典
- Block语法的使用
- Runtime.exec的一些事
- 内核调试方法 三
- 相爱五年。他残忍的对她说分手。她发誓恨他一辈子
- 优先队列LA3135(uva1203)
- openfire安装的过程中出现win7系统64位和32位的问题
- POJ1729 Jack and Jill (双向广度优先搜索, DBFS)
- shell 学习 笔记(二)
- FTP连接报530 User 用户名 cannot log in home directory inaccessible的解决方法
- android 蓝牙创建通讯链路的2种方法