Results 1 to 7 of 7
  1. #1
    seongjoo is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Invoke external program using ProcessBuilder

    Hello,

    I am trying to invoke an external program using ProcessBuilder. It seems pretty straight forward so I implemented some code to perform the task. But somehow the program is terminated without performing the specified task. The ProcessBuilder supposedly throws an exception if something is wrong but it didn't throw any exception and is just finished.


    Java Code:
    List<String> command=new ArrayList<String>();
    	
    command.add(ExecPath);		
    
    try {
    	ProcessBuilder pb = new ProcessBuilder(command);
    	Map<String, String> env = pb.environment();
    		
    	pb.directory(new File(workdir));			
    			
    } catch (IOException e) {
    [INDENT]e.printStackTrace();
    return false;[/INDENT]
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because you haven't actually started the process.

  3. #3
    seongjoo is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    I accidentally delete those part when posting the thread. But it's there.

    Java Code:
    try {
    			ProcessBuilder pb = new ProcessBuilder(command);
    			Map<String, String> env = pb.environment();			
    			
    			pb.directory(new File(workdir));
    			final Process process =pb.start();
    
    } ...

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You haven't told the program to wait for the process to finish. See the API docs for Process.

  5. #5
    seongjoo is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    You haven't told the program to wait for the process to finish. See the API docs for Process.
    Ah, that makes very much sense. I will try that and get back with the result. Thank you :)

  6. #6
    seongjoo is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    You haven't told the program to wait for the process to finish. See the API docs for Process.
    Well, process.waitFor() returns 1 so there is some problem executing the external program... Hmm...

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So read the output and error streams and find out what happened.

Similar Threads

  1. How Can I Set The Position Of External Program?
    By *Java* in forum New To Java
    Replies: 5
    Last Post: 03-14-2011, 11:33 AM
  2. How to invoke a C API using Java program
    By mgopi in forum New To Java
    Replies: 6
    Last Post: 12-27-2008, 01:17 PM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM
  5. Use a external file in my program
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:28 AM

Posting Permissions

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