Results 1 to 10 of 10
- 07-04-2011, 08:14 PM #1
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
Calling an external exe from Java code with some input file
I want to call a C exe from a Java code and want to pass an input file to that C exe as well.
I can call an exe through this code (without any input file)
Process myProcess = Runtime.getRuntime().exec("my.exe");
But I dont know, how to call my.exe with the input file "myfile.txt".
Thanks for the help.
regards,
- 07-04-2011, 08:17 PM #2
Are you saying that you want to read the file name from a text file?
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-04-2011, 08:32 PM #3
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
The exe "my.exe" uses the file "myfile.txt" as an input file.
For an instance, I use the following command to execute "my.exe" at the command prompt.
/usr/local/bin/my.exe < /Users/sal/myfile.txt
Now I want to call "my.exe" through Java code (and pass the input file myfile.txt to my.exe) and then want to capture the output of "my.exe" in Java code.
regards,
- 07-04-2011, 08:35 PM #4
Ok, I have no clue how you would accomplish this the way you're trying it. Have you tried using Java to write the information you need to transfer to a file and then have your my.exe read that information and then write its output to another file for Java to read?
It may be less then appealing but unless someone else here has any working knowledge on what you're trying to do I haven't the slightest clue to how you can do this.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
-
There are several overloads for the Runtime#exec(....) method, three that accept an array of Strings and might be useful for you. You'll probably also want to capture the C program's standard output stream in order to "capture" its output. The pitfalls of this are well described in this article that I highly recommend: When Runtime.exec() won't
- 07-04-2011, 09:22 PM #6
It sounds to me like you have a lot of reading ahead of you. I read the article and glanced at the API and it sounds to me like a fairly complicated journey you have made for yourself.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-05-2011, 04:18 PM #7
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
I tried overloaded version of Runtime#exec(....) but then some how, it does not works for me. The ProcessBuilder loads the executable (evmdd-smc) properly but unable to run that evmdd-smc over the input "model.stm" .
The code and error is given below.
public void callModelChecker(){
try {
ArrayList<String> command = new ArrayList<String>();
command.add("/usr/local/bin/evmdd-smc"); // this is exe
command.add("/Users/sal/model.stm"); // this is input to that exe
ProcessBuilder builder = new ProcessBuilder(command);
final Process p = builder.start();
InputStream stderr = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
}
catch (Exception err) {
err.printStackTrace();
}
}
It produces the following error through this statement (System.out.println(line);).
"Error: Unknown option /Users/sal/model.stm, ignoring!"
Any advise will be highly appreciated.
- 07-05-2011, 04:26 PM #8
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
Just to give you some idea of the working environment. evmdd-smc is a model checker (http://research.nianet.org/~radu/evmdd/) that checks the model described in model.stm file. On command prompt it works very well (as I mentioned in previous part of this post) but I could not manage to run it through Java code. My Java code generates that model (model.stm) and now I want to check that model by calling evmdd-smc:)
- 07-05-2011, 05:04 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Use a batch file and call that instead?
- 07-05-2011, 08:10 PM #10
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
Thanks for the help by all members. The solution told by Tolls works for me. I created a simple batch file with the following contents and it works for me
#!/bin/sh
/Users/sal/evmdd-smc < /Users/sal/model.stm
I am reproducing the code so that may be other people find it helpful.
public void callModelChecker(){
try {
ArrayList<String> command = new ArrayList<String>();
command.add(""/Users/sal/filename.sh"); // this is a batch file
ProcessBuilder builder = new ProcessBuilder(command);
final Process p = builder.start();
InputStream stderr = p.getErrorStream(); // or one may use getInputStream to get the actual output of process
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
}
catch (Exception err) {
err.printStackTrace();
}
}
Similar Threads
-
calling a .exe file from java code
By ingudam in forum New To JavaReplies: 1Last Post: 04-04-2011, 12:41 PM -
calling Assembly language code from java using JNI
By ashok_jeev in forum Advanced JavaReplies: 6Last Post: 02-07-2011, 07:43 AM -
Calling Java from C++ code
By franklyn@bellsouth.net in forum New To JavaReplies: 1Last Post: 11-25-2010, 10:06 AM -
can i remove a file from a input? can provided a full code for me?
By reeveliew in forum New To JavaReplies: 3Last Post: 05-06-2010, 07:10 AM -
calling c code from java
By sara12345 in forum New To JavaReplies: 14Last Post: 12-27-2009, 06:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks