Executing a .bat file from my code

Discussion in 'Java' started by Meidan, Dec 27, 2005.

  1. Meidan

    Meidan Guest

    Hi,

    Is there a way to execute a .bat file from within my java code?
    Something like WinExec?

    Thanks.
     
    Meidan, Dec 27, 2005
    #1
    1. Advertising

  2. Meidan wrote:

    > Is there a way to execute a .bat file from within my java code?


    <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String)>
    (and overloaded variants) though it might make more sense
    to 'exec' the individual commands in the .bat file of interest.

    HTH

    --
    Andrew Thompson
    physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
     
    Andrew Thompson, Dec 27, 2005
    #2
    1. Advertising

  3. Meidan

    Roedy Green Guest

    On 27 Dec 2005 01:12:52 -0800, "Meidan" <> wrote,
    quoted or indirectly quoted someone who said :

    >Is there a way to execute a .bat file from within my java code?
    >Something like WinExec?


    see http://mindprod.com/jgloss/exec.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 27, 2005
    #3
  4. Meidan

    Shameek Guest

    I have a sample code which shows how to run a .bat file (or any
    executable file from java). :

    import java.io.*;
    class test{
    public static void main(String arg[]){
    try{
    String command = "cmd /C start C:/test.bat ";
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(command);

    }catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    Here the .bat file is present in c:\test folder.
    Hope this will b of help to u.
     
    Shameek, Dec 28, 2005
    #4
  5. Meidan

    Guillaume Guest

    Meidan:
    > Is there a way to execute a .bat file from within my java code?
    > Something like WinExec?


    Something like:
    Runtime.exec("command.com /c your.bat");

    --
    My desktop is worth a million of dollars. Put an icon on it.
    http://www.milliondollarscreenshot.com/
     
    Guillaume, Jan 2, 2006
    #5
  6. Meidan

    Simon OUALID Guest

    Guillaume wrote:
    > Meidan:
    >
    >> Is there a way to execute a .bat file from within my java code?
    >> Something like WinExec?

    >
    >
    > Something like:
    > Runtime.exec("command.com /c your.bat");
    >


    Or cmd.exe for win2k / xp.

    More information on how to make a good use of the Runtime's exec method
    here :

    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
     
    Simon OUALID, Jan 3, 2006
    #6
  7. Meidan

    Joined:
    Jun 26, 2008
    Messages:
    1
    HI Guys
    I have tested following code this working on Win..XP, over java 1.5++.
    To test this code you need to create three different files.
    1. RunBatFile.java
    2. RunByJava.bat
    3. testFile.bat

    I hope it will help us to see execution of bat file using java.
    All the best.

    package batfile;
    import java.io.*;
    class RunBatFile{

    public static void main(String args[]){

    try {
    Runtime rt = Runtime.getRuntime();
    rt.exec("RunByJava.bat");
    System.out.println("Process exitValue: ");
    } catch (Exception e) {
    System.out.println("Unexpected exception Trying to Execute Job: " +e);
    }
    }
    }


    *************************************
    RunByJava.bat
    -------------------------
    echo off
    ECHO Starting TCP Monitor for Blue Martini
    START/MIN CALL testFile
    ***************************
    testFile.bat
    ----------------------
    set tDate=%DATE%

    ECHO todays date is %tDate%
    SLEEP 5
     
    , Jun 26, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Executing .bat file

    , Sep 15, 2006, in forum: Java
    Replies:
    2
    Views:
    510
    Rogan Dawes
    Sep 16, 2006
  2. JohnP
    Replies:
    3
    Views:
    659
    Hans Kesting
    Sep 26, 2007
  3. Tom Salzmann
    Replies:
    3
    Views:
    276
  4. Replies:
    1
    Views:
    94
    Si Ballenger
    Apr 22, 2006
  5. Replies:
    5
    Views:
    156
    Thomas 'PointedEars' Lahn
    Jun 26, 2005
Loading...

Share This Page