简体   繁体   English

使用Runtime.exec从Java运行进程-退出代码139

[英]Run process from Java with Runtime.exec - exit code 139

I want to run a C++ program from Java in Linux 我想在Linux中从Java运行C ++程序

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("/home/user/myProgram inputFilePath secondArgument");
int exitVal = pr.waitFor();

If I run the same command from a terminal everything works fine, the problem is when I run it from Java. 如果我从终端运行相同的命令,则一切正常,问题是当我从Java运行它时。 I am getting exit code 139. The program gets executed but something fails while executing. 我正在获取退出代码139。该程序得到执行,但是执行时出现故障。 Actually if I do not enter the inputFilepath the program is executed just fine and I can catch the message over the stdout about the "missing input file". 实际上,如果我不输入inputFilepath ,程序就会很好地执行,我可以通过stdout捕获有关“缺少输入文件”的消息。

Any idea? 任何想法?

SOLUTION: After some trial and error I found the problem. 解决方案:经过反复试验后,我发现了问题所在。 Apparently a "\\n" was needed at the end of the parameters, probably used as flag by the C++ program I was running. 显然,在参数末尾需要一个“ \\ n”,可能被我正在运行的C ++程序用作标志。 There was for sure a segmentation fault while parsing the arguments. 解析参数时肯定存在分段错误。

139 is the exit code that you'd see if the C++ program crashed with a segmentation fault. 139是退出代码,如果C ++程序因分段错误而崩溃,您将看到该退出代码。 This would indicate a memory access bug within the C++ program. 这将表明C ++程序中存在内存访问错误。 See Are there any standard exit status codes in Linux? 请参阅Linux中是否有任何标准的退出状态代码? .

Alternately, the program could have exited with exit code 139 for reasons of its own. 或者,该程序可能由于其自身原因以退出代码139退出。 Without knowing what the program is, it's not really possible to be more specific. 在不知道程序是什么的情况下,实际上不可能更加具体。

Did you check what this command returns in your Linux console? 您是否检查过此命令在Linux控制台中返回的内容? Try to run "/home/user/myProgram inputFilePath secondArgument; echo $?". 尝试运行“ / home / user / myProgram inputFilePath secondArgument; echo $?”。 139 on exit means the command returned 139 and it is not a problem of your Java code. 退出时返回139表示命令返回139,这不是Java代码的问题。

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

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