Java.Runtime.exec

Started by
3 comments, last by Concentrate 13 years, 3 months ago
I have this weird problem. I am using java to integrate this C++ program I have. I use java to call the C++ executable. The C++ executable is just a test dummy. This is the source :


int main(int argCount, char * args[]){
int sum = 0;
for(int i = 0; i < argCount; ++i){
sum += convertTo<int>(args);
}
ofstream fileout("result.txt");
cout << "Total Sum = " << sum << endl;
fileout<< "Total Sum = " << sum << endl;
return 0;
}


I am using the following java code to execute it :


Process p = null;
String path = "Program.exe";
String cmds[] = {path,"3","2","1"};
p = Runtime.getRuntime().exec(cmds);
init(p);
doRead();


and from the java output screen I get "Total Sum = 6" and it works fine. The problem is that notice the "fileout" in the C++ executable. The file isn't created and hence the
data isn't stored. I'm not sure how to solve this problem. Any ideas? I understand that java redirect the output stream into its outputstream or something, but I don't think thats exactly the problem, although it might be part of it. Any ideas?
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Advertisement
Bumping for help. The problem exist for also reading from file. For example, If I take a filename from the command line argument, and try to open it, I get an error saying cannot open it. The file exist so I know thats not the problem. This is a big issue for me. Please assist me. Thank you.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
I solved it. Whohohooo.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Care to enlighten us?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Sure. The problem was with netbeans. When I used the ProcessBuilder or the Runtime.exec it wasn't using the directory passed to find the file. Instead it was using its default project directory. So I could either set the environment path variable to the correct directory, or just create a copy of a file and add it to the default netbeans project directory. So if I use the ofstream to save data, the data was being saved in my netbeans project directory.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

This topic is closed to new replies.

Advertisement