Results 1 to 10 of 10
  1. #1
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default 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,

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  3. #3
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    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,

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  7. #7
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    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.

  8. #8
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    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:)

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Use a batch file and call that instead?

  10. #10
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    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

  1. calling a .exe file from java code
    By ingudam in forum New To Java
    Replies: 1
    Last Post: 04-04-2011, 12:41 PM
  2. calling Assembly language code from java using JNI
    By ashok_jeev in forum Advanced Java
    Replies: 6
    Last Post: 02-07-2011, 07:43 AM
  3. Calling Java from C++ code
    By franklyn@bellsouth.net in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 10:06 AM
  4. Replies: 3
    Last Post: 05-06-2010, 07:10 AM
  5. calling c code from java
    By sara12345 in forum New To Java
    Replies: 14
    Last Post: 12-27-2009, 06:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •