0

I want to run a C++ program from Java in Linux

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. I am getting exit code 139. The program gets executed but something fails while executing. 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".

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. There was for sure a segmentation fault while parsing the arguments.

Altober
  • 922
  • 2
  • 13
  • 27
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. – Andrew Thompson Mar 21 '15 at 01:51

2 Answers2

2

139 is the exit code that you'd see if the C++ program crashed with a segmentation fault. This would indicate a memory access bug within the C++ program. See Are there any standard exit status codes in Linux?.

Alternately, the program could have exited with exit code 139 for reasons of its own. Without knowing what the program is, it's not really possible to be more specific.

Community
  • 1
  • 1
Kenster
  • 18,710
  • 21
  • 68
  • 90
0

Did you check what this command returns in your Linux console? Try to run "/home/user/myProgram inputFilePath secondArgument; echo $?". 139 on exit means the command returned 139 and it is not a problem of your Java code.

Max Fomichev
  • 229
  • 1
  • 12