Java中运行DOS命令的问题
各位 我想在 Java 中执行 EXE 文件,但无法正确执行。 原来,在DOS命令提示符下,我的命令是这样的:
C:>\crf_test.exe model <inputfile.txt> outputfile.txt
注意:输入的文件名必须放在括号<>中。在 DOS 窗口中运行它时,它总是给我很好的结果。
当我希望我的java程序调用上面的命令时,我这样做:
Process p = Runtime.getRuntime().exec("crf_test.exe model <inputfile.txt> outputfile.txt");
但是,这个命令的输出是“没有这样的文件或目录:” 我猜 Java 不喜欢括号 <>在DOS命令中。我还删除了 <>出,但 exe 文件不接受这一点。 那么现在我该如何处理这个问题呢?请给我一个解决方案 非常非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可能存在两个问题。首先,其他人提到的 crf_test.exe 可能不在您的路径中。很难判断“没有这样的文件或目录”的输出是来自 crf_test.exe 还是来自 Java 试图查找 crf_test.exe。
其次,从 DOS 运行它,您并没有真正将文件名放在括号中 - 您正在将系统输入从输入文件重定向到输出文件,因此逻辑分组是:
现在,当您从 Java 运行它时,它确实尝试将
作为第二个命令行参数传递,而 outputfile.txt作为第三个。我的猜测是 crf_test.exe 然后尝试将它们加载为文件,并给出“没有这样的文件或目录”作为错误。您需要自己从 inputfile.txt 加载数据并通过 Process.getOutputStream 传入,然后从 Process.getInputStream 读取输出并将其写入到outputfile 。TXT。
或者,您可以运行 cmd.exe 并将整个命令作为参数传递 - 这样 shell 将为您执行重定向,就像您从 DOS 提示符运行一样。
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:
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 fromProcess.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.
尖括号是重定向运算符:outputfile.txt 导致输出写入到 outputfile.txt 而不是屏幕。此功能由 shell 提供,但是当使用 Java 运行时调用程序时,shell 不存在。通过 shell 调用,如下所示:
...或使用 Java 提供的工具重定向输入和输出;请参阅这个问题。
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:
...or redirect input and output using facilities provided by Java; see e.g. this question.
试试这个
Process p = Runtime.getRuntime().exec("crf_test.exe","/c","模型输出文件.txt");
try this
Process p = Runtime.getRuntime().exec("crf_test.exe","/c","model outputfile.txt");
问题是当前执行文件夹中缺少 crf_test.ext。您需要复制可执行文件或使用绝对路径或设置 PATH 变量以包含必要的路径。
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.如果我理解问题正确的话,你想打电话
,输入来自文件
inputfile.txt
并输出到outputfile.txt
您必须重新调整输入和输出并仅调用
crf_test.exe 模型. 如何将 stdin 重定向到 java Runtime 中描述了如何重定向进出.exec?
If I understand the problem right, you want to call
with input from file
inputfile.txt
and output tooutputfile.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 ?