Results 1 to 6 of 6
  1. #1
    xan.amini is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Unhappy Running an bat execution from Java

    Hi,

    I am trying to execute a .bat file (called startwatch.bat) from Java using the following code:

    Java Code:
    public static void main(String[]args){
            //.....
    	runCmd("startwatch.bat");		
            //.....
    }
    
    
    public static void runCmd (String cmd) {
    String str = null;
    try {
    	Process p = Runtime.getRuntime().exec(cmd);
    	BufferedReader stdInput = new BufferedReader(new 
    			InputStreamReader(p.getInputStream()));
    	System.out.println("Here is the standard output of the command: " + cmd);
    	while ((str = stdInput.readLine()) != null) {
    		System.out.println(str);
    	}
    }
    catch (IOException e) {
    	System.out.println("exception happened - here's what I know: ");
    		e.printStackTrace();
    		System.exit(-1);
    	}
    }
    The text inside startwatch.bat is as follows:
    Java Code:
    start java -classpath ../KNIME_inputs/bin KNIME_inputs
    This .bat file will run a different java script. Since this new java script will be monitoring my main script, I must have them running at the same time. When I run the main java script, it opens the bat file executing as planned. However, it does not allow the main java script to continue, as it is waiting for the termination of the bat file.

    Is there a way for me to force the main java script to move on, but to also keep the bat file running?

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default

    it opens the bat file executing as planned. However, it does not allow the main java script to continue
    What is the "it" you are referring to? The JVM or the OS?
    Where does the "main" program block waiting for completion of called program? You have a loop that would "wait"
    Could you put the code that blocks on its own thread?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default

    And just to add another one -
    what happens when you run that BAT file by itself from terminal?
    Does it go back to the prompt?

  4. #4
    xan.amini is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default

    Hi Norm,

    Can you please explain how I can put
    the code that blocks on its own thread?

  5. #5
    xan.amini is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default

    Tolls,

    Yes, when I run the BAT file by itself from the terminal it works perfect, executing the code in a separate cmd window and waiting for a new prompt in the original.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default

    To see how to put a section of code on its own thread, read the tutorial. Go here and Find thread:
    The Really Big Index

    and/or read the API doc:
    Java Platform SE 6

Similar Threads

  1. Query about JAVA Program Execution
    By annu in forum New To Java
    Replies: 2
    Last Post: 06-15-2011, 02:16 PM
  2. Replies: 10
    Last Post: 11-29-2010, 03:02 PM
  3. Script execution from java
    By umapathy_sekar in forum Advanced Java
    Replies: 7
    Last Post: 09-17-2010, 06:34 PM
  4. Replies: 0
    Last Post: 08-26-2009, 02:03 PM
  5. Need help in java program execution...
    By sachinmittal in forum New To Java
    Replies: 6
    Last Post: 10-10-2008, 03:37 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
  •