简体   繁体   English

从Java运行命令行的问题

[英]Problems with running command line from Java

I've been trying to run a compiled C program from Java using following commnand. 我一直在尝试使用以下命令从Java运行已编译的C程序。 Then I want to get results from process' input stream. 然后,我想从流程的输入流中获取结果。

Process p = Runtime.getRuntime().exec(commandLine);

Now, my command is this (first string is path to program, second and third are paths to files, which program takes as params): 现在,我的命令是这样的(第一个字符串是程序的路径,第二个字符串是文件的路径,程序将其作为参数):

trec_eval.8.1\trec_eval.exe trec_eval.8.1\czech TREC\results_2016-04-26_20_52_175.txt

When I run it normally from command line (I'm on Windows 10), it works as expected (C program finishes without errors and prints expected output into the command line), but alas, when I run it from Java, it doesn't work. 当我从命令行正常运行(我在Windows 10上)时,它可以按预期运行(C程序完成而没有错误,并将预期的输出打印到命令行中),但是,当我从Java运行它时,它没有运行。工作。 Both stdout and stderr streams of the process are empty and process' exit status is some error code. 进程的stdout和stderr流均为空,并且进程的退出状态是一些错误代码。

Here is minimal "working" example (I omitted stderr stream, since it made this code snippet too long): 这是最小的“有效”示例(我省略了stderr流,因为它使此代码段太长了):

String commandLine = "trec_eval.8.1\\trec_eval.exe" +
            " trec_eval.8.1\\czech + " " + file;
System.out.println("Command: " + commandLine);

Process process = Runtime.getRuntime().exec(commandLine);
BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));

StringBuilder output = new StringBuilder("Output:\n");
for (String line; (line = stdout.readLine()) != null; ) {
        output.append(line).append("\n");
}
System.out.println(output.toString());

int exitStatus = 0;
try {
    exitStatus = process.waitFor();
} catch (InterruptedException ie) {
    ie.printStackTrace();
}
System.out.println("Exit status: " + exitStatus);
stdout.close();

Output from this code are following lines: 此代码的输出是以下几行:

Command: trec_eval.8.1\trec_eval.exe trec_eval.8.1\czech TREC\results_2016-04-27_18_27_585.txt
Output:
Exit status: -1073741515

Obviously, I have read several other answers here on Stackoverflow and elsewhere. 显然,我已经在Stackoverflow和其他地方阅读了其他答案。 Unfortunately, code in said answers was pretty much the same as mine (and did not work for me in the same way as my code does not work). 不幸的是,答案中的代码与我的代码几乎相同(并且对我来说不起作用,因为我的代码不起作用)。

Can someone please tell me, what am I doing wrong? 有人可以告诉我,我做错了什么吗? Why is stdout stream empty instead of containing C program's output? 为什么stdout流为空而不是包含C程序的输出? Why does not the process exit with status 0 (as it should), but with before-mentioned monstrosity? 为什么进程不以状态0(应有的状态)退出,而是以前面提到的怪兽退出? Finally, if you know, why my Java code does not work as expected, can you explain, why the exact same command works from the the command line? 最后,如果您知道为什么我的Java代码无法按预期运行,您能否解释一下,为什么从命令行使用完全相同的命令?

Thanks in advance. 提前致谢。

It seems like the program is not getting something it needs in it's environment. 该程序似乎无法在其环境中获得所需的东西。 You said the program exited with an error code--so that sounds like your Java code is doing exactly what it should be doing (Starting the program and reading the exit code). 您说程序退出时带有错误代码-听起来您的Java代码正在正确执行应做的事情(启动程序并读取退出代码)。

Your paths look relative--perhaps Java isn't starting in the directory you think it is? 您的路径看起来是相对的-也许Java不在您认为是的目录中启动? Try doing a full path to your argument and see if that helps. 尝试为您的论点提供完整的路径,看看是否有帮助。

Is there a way you can interpret the error code from the app? 有没有办法可以从应用程序解释错误代码?

If all else fails, try running it through a shell (either cmd /c or sh depending on your OS) using full paths. 如果所有其他方法均失败,请尝试使用完整路径通过外壳程序(取决于您的操作系统,使用cmd / c或sh)运行它。 You can test that without java, then pass the whole thing to java and see if you get the same results. 您可以在没有Java的情况下进行测试,然后将整个内容传递给Java,看看是否得到相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM