How can I execute a  .bat  file from inside a java application

I have a .bat file which contains an executable file(.exe) and some input and output file names. What commands can I use to execute this bat file from my java application.

After raeding tkleisas' reply; i am trying to invoke another application (which can be invoked from the command line) by using a batch file and trying Runtime.exec for executing a batch file.
My current code is:
Runtime runtime = Runtime.getRuntime();
Process trialProcess;
trialProcess = runtime.exec("cmd.exe /C start C:\\guns.bat /B");
And my guns.bat looks like:
cd C:\CALPUFF
echo trial
start calpuff.exe CALPUFF.INP
This is not working for me and i get the following in the error stream of the trialProcess:
Error :
The system cannot execute the specified program.
Process finished with exit code 1
Has anyone come across something like this and know what's wrong with this one??
thnx

Similar Messages

  • How can I execute a batch file from my java program

    Hi,
    Can someone help me or direct me to a link,
    How can I execute a DOS batch file from my java program?
    Thanks

    You will need to grab a handle to the process's
    outputstream so u can see its output.The OP didn't mention any output from any batch file;
    nor any input for that
    matter,so lets not complicate matters here for now
    ok?Actually I think this is essential to see whether it works or not. It's either that or do some manual check to see whether it ran, which is not exactly elegant, and in some cases this may not be easier than simply writing the output stream code, or in fact it may be impossible to check manually.
    I'm sure it wasn't intentional that your post appeared to be bristling with attitude.

  • How can I run a .bat file from an html/javascript adobe air installation package?

    I write an html/javascript code which works completely true
    but when I made the installation package by air-sdk
    it dosent work properly
    my code is:
    <html>
    <head>
        <title>Hello World</title>
              <script type="text/javascript">
            function appLoad() {
                air.trace("Hello World");
        </script>
    </head>
    <body onLoad="appLoad()">
        <h1>Hello World</h1>
              <br />
    <button onclick="window.open('file:///C:/Windows/notepad.exe')"> Launch notepad </button>
    <button onclick="window.open('file:///D:/opennotepad.bat')"> Launch batnote </button>
    </body>
    </html>
    Chapter 5: Creating your first HTML-based AIR application with the AIR SDK

    Seems to be security violation. You can't execute bat or cmd

  • How to run Executables or .bat files from Java?

    Hi!
    I need to open a Web Browser from Java App.
    Please, help me to do this without calling JNI
    -or-
    provide me with sample which works with
    Runtime.execute() method.
    I have done full permission:
    grant
         permission java.security.AllPermission "","";
    };Thank You in advance!

    To run any command from java code, the method is
    Runtime.getRuntime().exec( myCommandString )
    Where, myCommandString is something like "/full/pathname/command".
    If the pathname contains spaces, e.g. "c:\program files\windows\notepad", then enclose it in quotes within the quoted string. Or pre-tokenize them into elements of an array and call exec(String[] cmd) instead of exec(String cmd).
    From JDK1.3 there are two new overloaded Runtime.exec() methods. These allow you to specify starting directory for the child process.
    Note, there is a gotcha associated with reading output from commands. When the runtime exec's the process, it passes to it 3 streams, for stdin, stdout, and stderr; the out and err are buffered but the buffer size isn't very big. When your process runs, it reads (if needed) from in, and writes to out and err.
    If it doesn't write more than the buffer-size, it can run to completion.
    But if it tries to write more data to one or the other stream than the buffer can hold, the write blocks, and your process hangs, waiting for you to empty the buffer so it can write some more.
    So after the exec call, get the streams, and read from them in a loop until they both hit end-of-stream (don't block on either one, just read whatever is available from each, each loop iteration).
    Then when the streams have ended, call the process.waitFor() method to let it finish dying.
    Now, here is a code snippet how you achieve this.
    String strCommand = "cmd.exe /c " + strCommand;
    boolean bWait = true;
    //execute the command
    try
         Runtime r = Runtime.getRuntime();
         Process pr = r.exec(strCommand);
         Process pr = r.exec(callAndArgs);
         BufferedInputStream bis =new BufferedInputStream(pr.getInputStream ());
         int c=0;
         /** Outlet for IO for the process **/
         while (c!=-1)
              c=bis.read();
         /**Now wait for the process to get finished **/
         if(bWait == true)
              pr.waitFor();
              pr.destroy();
    catch(Exception e)
         System.out.println("Could not execute process " + strCommand);
         return(false);

  • Executing a .bat file from java code

    Im writing a utility and i need to be able to create and execute a .bat file from my program. I can create the .bat file fine but when i try to execute it nothing happens. I tried to use the following line of code to try to execute the .bat file (called make.bat) :
    try
    String parameterString = "\"C:\\WINNT\\System32\\cmd.exe\" C:\\ITS_Test\\make.bat";
    Runtime.getRuntime().exec(parameterString);
    catch (Exception e)
    System.out.println(e);
    The Runtime.getRuntime()exec() method worked fine when trying to execute an excel file but for some reason it dosen't want to work for the DOS prompt. Any ideas?

    Thanks everyone for all the replies.
    I have tried all the above with no luck. Im using netbeans, does anyone think that it could be a problem with the IDE.
    I tired the following code :
    String parameterString = "cmd.exe /C \"C:\\ITS_Test\\make.bat\"";
    Process pr = Runtime.getRuntime().exec(parameterString);
    pr.waitFor();
    This code does not throw any exceptions but just hangs. I also tried:
    String [] parameterString = {"cmd", "/c", "C:\\ITS_Test\\make.bat"};
    Process pr = Runtime.getRuntime().exec(parameterString);
    pr.waitFor();
    and:
    String parameterString = "\"C:\\WINNT\\System32\\cmd.exe\" \"C:\\ITS_Test\\make.bat\"";
    Process pr = Runtime.getRuntime().exec(parameterString);
    pr.waitFor();
    with the same result. Does anyone know why it just hangs and nothing happens (no exceptions thrown)?

  • How can I move an icon/file from one screen of my ipad to another?

    How can I move an icon/file from one screen on my ipad to another?

    Press and hold the icon until they start to wiggle. Then while holding it, drag it to the edge of the screen and wait for the page to switch, then let go of the app

  • How can  I checkout an existing file from CVS without going through the wiz

    How can I checkout an existing file from CVS, that is not already in my local jdev, without going through the wizard and checkingout everything?

    You can't. AI doesn't produce .PLT files.

  • How can I transfer a sound file from my "Voice Memos" app on my iPhone to my iPad?

    How can I transfer a sound file from my "Voice Memos" app on my iPhone to my iPad?

    In iTunes with your iPhone connected, click on the iPhone device, select the Apps tab, scroll down to the File Sharing section and pick an app that plays Quicktimes ("Files Connect" works in this example, but I'm open to suggestions of better apps for playing transferred Quicktimes) and drag your Quicktime from Finder to the Documents pane for the File Sharing app.   Once the file transfer is complete, go to the app on your iPhone to play the Quicktime.

  • How can I prevent a PDF file from being copied, printed or downloaded? Students should only be able to view the text and and not distribute it in any way.

    How can I prevent a PDF file from being copied, printed or downloaded? Students should only be able to view the text and and not distribute it in any way.

    You can prevent it from being printed by applying a security policy to it
    in Acrobat. The rest can't be prevented, unless you spend  a LOT of money
    on DRM protection.

  • How can open a html, pdf files from the server (c:\foldername\filename)

    Hi all,
    I am developing an application. in this app i have to implement RMI.
    My problem is this, How can a client open a files from the server.
    all the html, pdf files are stored in the server's directory like C:\audit\filename.
    There is a folder on the server's C directory which contains all the html , pdf files.
    please tell me how can a client open a file from the server using RMI.
    Please send me the solution as soon as possible.
    Thanks & Regards
    Bhavishya

    That's doing it the hard way. Why not use HTTP or FTP?

  • I have converted a pdf to word. How can I access the Word file from my online account?

    I have converted a pdf to word. How can I access the Word file from my online account? When it says 'download the converted file' I choose a location on my PC and click, but nothing happens. It seems that it can only save the converted file to my online account. I went to my online account but I see no way to look for the file

    Hey Fabrizio,
    You might need to sign up at "https://cloud.acrobat.com/exportpdf" using your Adobe ID credentials to convert your PDF file to Word.
    Do you get the 'download' prompt?
    Also, you can find the converted files by clicking at the 'Files' tab. 
    Please try the same using a different browser and check.
    Hope to hear from you.
    Regards,
    Anubha

  • How can I delete two .jpg files from my desktop that refuse to be deleted?

    How can I delete two .jpg files from my desktop that refuse to be deleted?

    The two icons refused to be moved to the garbage. The good part in all this is that ater I turned off the computer at night and turned it back on the next morning they were gone!
    Thanks.

  • How can I copy all the files from my iMac to an external drive so they remain as 'regular accessible files'? I want to clean my dive

    How can I copy all the files from my iMac to an external drive so they remain as 'regular accessible files'? I want to clean my dive

    You can use a program like Carbon Copy Cloner.  It will clone your HD to the external.  The format is the same as your internal drive and you can boot from it using Option key at boot time.
    http://www.bombich.com/
    Regards,
    Captfred

  • How can I transfer all my files from Mavericks Thunderbird to Mavericks Mail

    How can I transfer all my files from Mavericks Thunderbird to Mavericks Mail?
    Bonjour,
    C'est ma première intervention aussi je ne sais pas si je peux m'exprimer en français.
    Je tente!
    Lors du passage à Maveicks, j'ai basculé sur Thunderbird car je ne souhaitais pas utiliser POP mais Mail ne le permettait pas (du moins je n'ai pas réussi)
    Aujourd'hui, je souhaite revenir sur Mail et, pour celà, je dois rapatrier toutes mes données de messagerie depuis Thunderbird.
    J'utilise Imap et, pour gérer ma messagerie, j'ai créé des boîtes aux lettres dans Local Folsders
    Le passage par Eudora ne fonctionne pas avec Mavericks.
    L'import de boîtes aux lettres depuis Local Folders de Thunderbird (via le menu Fichier) ne permet de transférer qu'un seul courier par boîte aux lettres de Local Folders. Et c'est ce qui m'empêche de passer à Mail.
    Merci d'avance pour votre aide.

    iPhone: Transferring information from your current iPhone to a new iPhone

  • How can I save adobe reader file from gmail into my adobe reader app?? i would like to open one file in place where I'm not able to access internet and for that reason i would like to save it. is that even possible?

    How can i save adobe reader file from my gmail into my adobe reader application. or is there any other way i can save it into my phone so I'm able to open in any situation not just from my gmail???

    Long hold on the document in mail and it should give you the open in... option, select adobe reader and the file is now saved locally for viewing even while offline.

Maybe you are looking for