0

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

Mofi
  • 46,139
  • 17
  • 80
  • 143
An Nguyen
  • 31
  • 6
  • Have you tried `exec(new String[]{"crf_test.exe","model","","outputfile.txt"})`? – rodion Jan 12 '11 at 08:27
  • Alternatively try using ".\\crf_test.exe ..." instead of "cft_test.exe ...". I had a similar problem on Linux before and this solved it. – rodion Jan 12 '11 at 08:28
  • 2
    See [Java exec() does not return expected result of pipes' connected commands ](http://stackoverflow.com/questions/2088917/java-exec-does-not-return-expected-result-of-pipes-connected-commands). That question addresses pipes, but it's really the same thing. Both pipes and redirections (<, >) are functions of the shell, and not handled by `exec`. This [article](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4) is also helpful, particularly the "Runtime.exec() is not a command line" section. – Matthew Flaschen Jan 12 '11 at 08:29
  • +1 Good point. This would have been the next problem s/he'd face) – rodion Jan 12 '11 at 08:32

5 Answers5

4

There are potentially two problems. Firstly, the one that others have mentioned - crf_test.exe may not be in your path. It's hard to tell whether your output of "no such file or directory" is from crf_test.exe or from Java trying to find crf_test.exe.

Secondly though, running it from DOS you're not really putting the filename in brackets - you're redirecting system input from the input file to the output file, so the logical grouping is:

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

Now when you're running it from Java, it's genuinely trying to pass <inputfile.txt> as a second command-line argument, and outputfile.txt as a third. My guess is that crf_test.exe is then trying to load those as files, and giving "no such file or directory" as an error.

You'll need to load the data from inputfile.txt yourself and pass it in via Process.getOutputStream, and then read the output from Process.getInputStream and write it to outputfile.txt.

Alternatively, you could run cmd.exe and pass in the whole command as an argument - that way the shell will perform the redirection for you as if you were running from a DOS prompt.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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.

Community
  • 1
  • 1
moonshadow
  • 86,889
  • 7
  • 82
  • 122
1

try this Process p = Runtime.getRuntime().exec("crf_test.exe","/c","model outputfile.txt");

Neha Milak
  • 11
  • 1
0

The problem is that the crf_test.ext is missing in your current execution folder. You need either copy the executable or use the absolute path or set the PATH variable to include the necessary path.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

If I understand the problem right, you want to call

crf_test.exe model

with input from file inputfile.txt and output to outputfile.txt

You have to redrect in and out and call only crf_test.exe model. How do redirect in and out is described in how to redirect stdin to java Runtime.exec ?

Community
  • 1
  • 1
Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94