1

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?

assylias
  • 321,522
  • 82
  • 660
  • 783

2 Answers2

1

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.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

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

piet.t
  • 11,718
  • 21
  • 43
  • 52
  • @user1629420: You're right, I updated my suggestion - it does work better in my testcase... – piet.t Sep 04 '13 at 13:07