Execute .bat file from java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    Execute .bat file from java

    I need to run a .bat file from java. This is what I got:

    public class CallingBatch {
    public static void main(String[] args) {
    Runtime run = Runtime.getRunt ime();
    try {
    run.exec("cmd start /c C:/batfile.bat");
    } catch (Exception e) {
    e.printStackTra ce();
    }
    System.out.prin tln("FINISHED") ;
    }

    }

    Now, the thing is I don't get any error's or any other type of feedback, nothing happens.

    Any suggestions?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by MarkoKlacar
    I need to run a .bat file from java. This is what I got:

    public class CallingBatch {
    public static void main(String[] args) {
    Runtime run = Runtime.getRunt ime();
    try {
    run.exec("cmd start /c C:/batfile.bat");
    } catch (Exception e) {
    e.printStackTra ce();
    }
    System.out.prin tln("FINISHED") ;
    }
    }

    Now, the thing is I don't get any error's or any other type of feedback, nothing happens.

    Any suggestions?
    1.) Use code tags when posting code.
    2.) What was the program supposed to do?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by MarkoKlacar
      I need to run a .bat file from java. This is what I got:

      [code=java]
      public class CallingBatch {

      public static void main(String[] args) {
      Runtime run = Runtime.getRunt ime();
      try {
      run.exec("cmd start /c C:/batfile.bat");
      } catch (Exception e) {
      e.printStackTra ce();
      }
      System.out.prin tln("FINISHED") ;
      }

      }
      [/code]

      Now, the thing is I don't get any error's or any other type of feedback, nothing happens.

      Any suggestions?
      [code=java]
      Process p = run.exec("C:/batfile.bat"); //Correct this line.
      //Add this line.
      System.out.prin tln(p.exitCode( ));
      [/code]
      Good Luck.

      Kind regards,
      Dmjpro.

      Comment

      • questionit
        Contributor
        • Feb 2007
        • 553

        #4
        Just a bit on executing a batfile.

        .bat file is self-executable. so you dont need to open command prompt first to run .bat file.

        If you do want to open cmd to run a .bat file or any other file, then try this example:

        [code=java]
        Process p = run.exec("cmd /k ipconfig");
        [/code]

        The above argument in run.exec will run cmd first and then run ipconfig.

        Qi

        Originally posted by dmjpro
        [code=java]
        Process p = run.exec("C:/batfile.bat"); //Correct this line.
        //Add this line.
        System.out.prin tln(p.exitCode( ));
        [/code]
        Good Luck.

        Kind regards,
        Dmjpro.

        Comment

        • MarkoKlacar
          Recognized Expert Contributor
          • Aug 2007
          • 296

          #5
          Thanks for replying.

          The program was supposed to call a batch file that does some basic stuff (just copy's some files, but that's not the important thing running the batch file is).

          When I added the lines I got this message:

          Code:
          java.lang.IllegalThreadStateException: process has not exited
          	at java.lang.ProcessImpl.exitValue(Native Method)
          	at callingBatch.CallingBatch.main(CallingBatch.java:10)

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by MarkoKlacar
            Thanks for replying.

            The program was supposed to call a batch file that does some basic stuff (just copy's some files, but that's not the important thing running the batch file is).

            When I added the lines I got this message:

            Code:
            java.lang.IllegalThreadStateException: process has not exited
            	at java.lang.ProcessImpl.exitValue(Native Method)
            	at callingBatch.CallingBatch.main(CallingBatch.java:10)
            Why don't you go through a tutorial on how to use Runtime.exec first?
            You are just falling into the most common and simple mistakes that most people make when using it.

            Comment

            • MarkoKlacar
              Recognized Expert Contributor
              • Aug 2007
              • 296

              #7
              Originally posted by r035198x
              Why don't you go through a tutorial on how to use Runtime.exec first?
              You are just falling into the most common and simple mistakes that most people make when using it.
              Ok, I'll try that. Thanks for pointing me in the right direction.

              Comment

              • cnarun6897
                New Member
                • Jan 2010
                • 1

                #8
                running bat file using java

                Hi folks...
                I too had the same problem. I first used as
                Runtime.getRunt ime().exec("cmd .exe start /c test.bat");
                Then I tried as below. It works fine.
                Runtime.getRunt ime().exec("cmd .exe /c start test.bat");

                Try this... Have fun...

                Comment

                • prathimmi
                  New Member
                  • Jul 2010
                  • 1

                  #9
                  HI All,

                  I have to convert the .DBF to .CSV file by using DBF2CSV.bat.
                  in JAVA.
                  After googled i got the following code . But it is not working ...


                  Please help me
                  String[] commands={"C:\\ DBF2CSV\\DBF2CS V.BAT","C:\\DBF 2CSV\\J006MD30. DBF"};
                  Process p = Runtime.getRunt ime().exec(comm ands);

                  Comment

                  Working...