• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help with running a C++ executable by using Runtime.exec

 
D Chhetri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to run a C++ executable using java. I am having a weird problem. The following is the java code I'm using to test the executable :


I get the following output in netbeans :

Running App
Exit Code: 0


So it seems that it worked and executed, but it didn't quite work correctly. The executable program, "Program.exe" takes in the arguments and process it, and ouputs the
data into a file called result.txt. So I was expecting a result.txt in the same directory as the executable but there is nothing there. If I click on the executable directly,
it generates the result.txt, but running it using the above code, it generates nothing, meaning that I don't see the result.txt file. It like it wasn't actually called or something.
But looking at the task manager, I see that it is being called. So I'm am desperate and asking you guys for help on what to do. I have been killing my self for hours and hours. This isn't homework or anything, I just need to do this in order to run other programs, so this is like a test trial program. Thanks for reading this long message.


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"No First Name No Last Name",
please check your Private Messages for an important administrative matter.
 
D Chhetri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that. Fixed.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) It is usually better to use
String cmds[] = {path, "1", "2","3"};
and then you won't need the quoting.

2) You can set the working directory using the third argument to the exec () method and specifying the second argument as 'null'. Using ProcessBuilder there is a method to define it.

3) Since the file paths are being handled by the OS and not by Java you should make sure you use '\' as file separator in Windows. . There are times when Windows tolerates either but Linux is very intolerant so get into the habit of using the correct file separator.

4) You need to read the 4 sections of http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html and implement all the recommendations. Though you seem to be getting away with not handling the process stdout and stderr one day they will jump up and bit you.

5) It is normally better to use ProcessBuilder than Runtime.exec() since it presents a better interface. Note that Runtime.exec() uses ProcessBuilder behind the scenes so they are directly equivalent.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic