简体   繁体   English

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

[英]Runtime.exec can't work on JDK 7u25

After update java to latest version 7u25, the runtime.getruntime().exec can't work anymore. 将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()));

I already read the reference:JDK 7u25: Solutions to Issues caused by changes to Runtime.exec https://blogs.oracle.com/thejavatutorials/entry/changes_to_runtime_exec_problems 我已经阅读了以下参考文献:JDK 7u25:由Runtime.exec更改引起的问题解决方案https://blogs.oracle.com/thejavatutorials/entry/changes_to_runtime_exec_problems

and tried some modifications as below: 并尝试了如下修改:

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

and this: 和这个:

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

and this: 和这个:

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

and this: 和这个:

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

I even replace the "jhghai_w.filepath" to "C:\\aucs\\data\\tmp.txt" directly. 我什至直接将“ jhghai_w.filepath”替换为“ C:\\ aucs \\ data \\ tmp.txt”。 But the are not working. 但是,这是行不通的。 What's the problem in my modification? 我的修改有什么问题?

You should not be using Runtime.exec() to begin with, for practical purposes is deprecated. 出于实用目的,不建议您首先使用Runtime.exec() Better switch to using ProcessBuilder . 最好改用ProcessBuilder There are plenty of tutorials to show you the way. 很多 教程向您展示方法。

You should pass your command to Runtime.exec() or your ProcessBuilder as a String-Array with three elements: the command as the first, "/C" as the second and the command to be executed in cmd as the third element: 您应该将命令作为包含三个元素的String-Array传递给Runtime.exec()或ProcessBuilder:第一个命令为命令,第二个命令为“ / 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();

See also this blogpost especially this section: 另请参阅此博客文章,尤其是本节:


The Golden Rule: 黄金法则:

In most cases, cmd.exe has two arguments: "/C" and the command for interpretation. 在大多数情况下,cmd.exe具有两个参数:“ / C”和用于解释的命令。


Edit: updated solution.... 编辑:更新的解决方案...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM