Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



run bat file from java program


2 replies to this topic - - - - -

#1 AdverseDeviant

    Resident Elite

  • 1,772 posts
  • Joined: 11-February 06
  • Location: VA, USA

Posted 02 May 2012 - 05:11

i have a bat file that i want to run from a java program that creates that bat file. Ive looked around but i dont see anything straight forward. something about a runtime class? Can anyone help me out? Thanks.


#2 MrChris2000

    Neowinian

  • 35 posts
  • Joined: 19-February 09

Posted 02 May 2012 - 05:53

Ok, here's a quick example that runs an external command and outputs the exit code.
One thing to bear in mind here is that the 'waitFor' method means the parent app will halt until the child app has completed (useful if you need the output to make decisions from prior to resuming).
This was just a quick google, more detail can be found here (including a non-halting example): http://www.rgagnon.c.../java-0014.html

import java.io.*;
public class CmdExec {
  public static void main(String argv[]) {
    try {
	  String line;
	  Process p = Runtime.getRuntime().exec("test.cmd");
	  p.waitFor();
	  System.out.println(p.exitValue());
    }
    catch (Exception err) {
	  err.printStackTrace();
    }
  }
}


#3 OP AdverseDeviant

    Resident Elite

  • 1,772 posts
  • Joined: 11-February 06
  • Location: VA, USA

Posted 02 May 2012 - 22:31

thank you