aspose file tools*
The moose likes Java in General and the fly likes Please Help with running a C++ executable by using Runtime.exec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please Help with running a C++ executable by using Runtime.exec" Watch "Please Help with running a C++ executable by using Runtime.exec" New topic
Author

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

D Chhetri
Greenhorn

Joined: Jan 12, 2011
Posts: 2
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

"No First Name No Last Name",
please check your Private Messages for an important administrative matter.


[My Blog]
All roads lead to JavaRanch
D Chhetri
Greenhorn

Joined: Jan 12, 2011
Posts: 2
Sorry about that. Fixed.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help with running a C++ executable by using Runtime.exec
 
Similar Threads
Runtime.exec("c.bat")
Runtime.exec() file problem
Running Batch Files
getting path of a file
Problem calling piped perl routine