Runtime.exec无法在JDK 7u25上运行

时间:2013-09-03 16:38:15

标签: java process java-7

将java更新到最新版本7u25后,runtime.getruntime()。exec不再有效。

//jhghai_w.filepath = "C:\\aucs\\data\\tmp.txt";
br = new BufferedReader(new InputStreamReader(Runtime.getRuntime()
                    .exec("CMD.EXE /C \"C:\\Program Files\\juman\\juman.exe \" -e < "+jhghai_w.filepath)
                    .getInputStream()));

我已经阅读了参考资料:JDK 7u25:Runtime.exec更改导致的问题解决方案 https://blogs.oracle.com/thejavatutorials/entry/changes_to_runtime_exec_problems

并尝试了一些修改如下:

br = new BufferedReader(new InputStreamReader(Runtime.getRuntime()
                    .exec("CMD.EXE /C \"C:\\Program Files\\juman\\juman.exe  -e < \""+jhghai_w.filepath)
                    .getInputStream()));

和此:

br = new BufferedReader(new InputStreamReader(Runtime.getRuntime()
                    .exec(new String[] {"cmd","/C" "C:\\Program Files\\juman\\juman.exe"-e < ",jhghai_w.filepath})
                    .getInputStream()));

和此:

br = new BufferedReader(new InputStreamReader(Runtime.getRuntime()
                    .exec(new String[] {"cmd","/C" "C:\\Program Files\\juman\\juman.exe","-e“,”<",jhghai_w.filepath})
                    .getInputStream()));

和此:

br = new BufferedReader(new InputStreamReader(Runtime.getRuntime()
                    .exec(new String[] {"cmd","/C" "\"C:\\Program Files\\juman\\juman.exe"","\"-e < \"",jhghai_w.filepath})
                    .getInputStream()));

我甚至直接将“jhghai_w.filepath”替换为“C:\ aucs \ data \ tmp.txt”。 但是没有用。我的修改有什么问题?

2 个答案:

答案 0 :(得分:1)

您不应该使用Runtime.exec()开头,因为实际应用已弃用。最好切换到使用ProcessBuilderplenty tutorials为您指明道路。

答案 1 :(得分:1)

您应该将命令传递给Runtime.exec()或ProcessBuilder作为具有三个元素的String-Array:作为第一个命令,“/ C”作为第二个,将命令作为第三个在cmd中执行元素:

String[] command = new String[3];
command[0] = "CMD.EXE";
command[1] = "/C";
command[2] = "\"C:\\Program Files\\juman\\juman.exe \" -e < "+jhghai_w.filepath;
ProcessBuilder pb = new ProcessBuilder(command);
pb.start();

另见this blogpost,尤其是本节:


黄金法则:

在大多数情况下,cmd.exe有两个参数:“/ C”和解释命令。


编辑:更新的解决方案......