开发者

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


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.


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.


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


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.


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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜