user1629420
user1629420

Reputation: 21

Runtime.exec can't work on JDK 7u25

After update java to latest version 7u25, the runtime.getruntime().exec can't work anymore.

//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

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. But the are not working. What's the problem in my modification?

Upvotes: 1

Views: 952

Answers (2)

piet.t
piet.t

Reputation: 11911

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[] 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.


Edit: updated solution....

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236112

You should not be using Runtime.exec() to begin with, for practical purposes is deprecated. Better switch to using ProcessBuilder. There are plenty of tutorials to show you the way.

Upvotes: 1

Related Questions