Skip to content

Problem with run DOS command in Java

Dear all I want to execute a EXE file in Java, but I was not able to do it correctly. Originally, in DOS command prompt, my command is like this:

C:>crf_test.exe model <inputfile.txt> outputfile.txt

Note: the input file name must be place in the brackets <>. It always gave me good results when run it in DOS window.

When I want my java program to call above command, I do like this:

Process p = Runtime.getRuntime().exec("crf_test.exe model <inputfile.txt> outputfile.txt");

However, the output of this command is “no such file or directory: ” I guest Java doesn’t like the brackets <> in DOS command. I also remove <> out, but exe file did not accept that. So now how can I deal with this problem? Please give me a sollution Thank you much much



The angle brackets are redirection operators: <inputfile.txt causes the input to be read in from inputfile.txt instead of the keyboard, >outputfile.txt causes the output to be written to outputfile.txt instead of the screen. This facility is provided by the shell, however when invoking your program with the Java runtime the shell is not present. Invoke via the shell, like this:

Runtime.getRuntime().exec("cmd /c crf_test.exe model <inputfile.txt> outputfile.txt");

…or redirect input and output using facilities provided by Java; see e.g. this question.

3 People found this is helpful