Batch file program

I want to write a java program that will ask the user for a file name, then it will open and execute the batch file that was entered. Is this possible?

This demo will run a batch file in the c:\ root. Play with it and i'm sure you will come up with something more advanced than this demo.
// Author List:
//      deAppel                         <Creator>
// Description:
//     Execute a bat with user input.
// Environment:
//      This software was developed using Eclipse and Java 1.6
// Copyright Information:
//      Copyright (C) 2007      <Institution><None>
//          Ark de Appel                www.engineeringserver.com
import java.io.IOException;
import javax.swing.JOptionPane;
public class RunBat {
     public RunBat(){
          String input = "Run batch file";               
          input = JOptionPane.showInputDialog(input);
          try {
               Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\cmd.exe /c start c:\\" + input + ".bat");               
          } catch (IOException e) {
               e.printStackTrace();
     public static void main(String[] args) {
          new RunBat();
}

Similar Messages

  • Running a java program via a batch file

    I am unable to run a java program from a batch file that I created.
    spiderpackage.EntryPoint is a class file which I am trying to run with a -v option to output something.What should I do to get the output?
    echo ^<html^>^<body^>
    echo hello^<br^>
    call java spiderpackage.EntryPoint -v
    echo ^</body^>^</html^>

    This has nothing to do with java programming. Have a look at the windows help for the call command.
    The echo <html> stuff doesn't make sense. What's it for?
    The command in you batch file should be:
    java -cp . spiderpackage.EntryPoint -v
    assuming that java is in the system path, and the EntryPoint.class is in a directory called spiderpackage which is a subdirectory of your current working directory

  • How to create a batch file for a java program?

    I have a java project in JCreator and the project is organised into packages. I have also configured JCreator to provide 2 arguments to the main method when the project is executed.
    I would want to create a batch file (.bat) for this project where the batch file can automatically use the arguments set in JCreator when the program runs.
    How do I go about creating the batch file?

    where the batch file can automatically use the arguments set in JCreatorThat all depends JCreator... can you get those arguments somehow?

  • URGENT:- Running a batch file from java program

    Author: pkanury
    I am trying to execute a batch file from my java program using RunTime and Process . It can execute any dos command except for a batch file. Can anyone throw
    some light ?? My code looks like this .....
    cmd = "command.com /c X:\\grits\\scripts\\test.bat"; Runtime rt = Runtime.getRuntime(); Process p = rt.exec(cmd);
    The weird part is that p.waitFor() returns a status of 0 implying that the cmd has been executed successfully. But that is not the case. And my batch file is as simple as - type "ADADA" > junk.txt
    Any help would be appreciated.

    I think it should work when you use a String[] array
    instead of a single String:
    String[] cmd = { "command.com", "/C", "D:\\batch\\do.bat" };
    Note: If the batchfile creates any output (i.e. files),
    they will be stored in the directory of the application
    which calls the batch file, not in d:\batch\...

  • To run a java program via a batch file,when called from a web server

    REM This batch file runs the Spider with the [-v] option.
    REM Lines 51 through 54 are simple DOS commands..they call the Spider
    REM The location of the Spider package is the important piece of information here...
    REM a batch file has access to the whole computer where the server resides.
    setlocal
    set JDK=C:\j2sdk1.4.1_01
    set PATH=%JDK%\bin;.;%PATH%
    CLASSPATH=%JDK%\jre\lib\*.jar;.;%JDK%\jre\lib\ext\*.jar;.;%CLASSPATH%
    java spiderpackage.EntryPoint -v
    endlocal
    I am trying to run this batch file from my web server.I can see nothing on my browser.I can run the same program from my machine but unable to run from web server.what should I do?

    I am making my question more clear...
    I have a Windows batch (*.bat) file that echoes some html, then calls a java program the output of which is html as well, then echoes the final html.
    To make sure that the java program gets properly executed, I set the necessary environment variables (e.g., JDK, PATH, CLASSPATH, etc.). The script looks something like this:
    setlocal
    set JDK=path_to_JDK
    set PATH=path_to_java
    set CLASSPATH=path_to_java_classes
    echo ^<html^>^<body^>
    call java foo
    echo ^</body^>^</html^>
    endlocal
    The idea, of course, is to call this script through a browser, on a web server.
    Curiously, both echo lines return to the browser just fine, but any output from
    the java program (which just writes html to STDOUT) does not make it back.
    Can someone explain to me why this is happening? How do I set things up so that the output (STDOUT and STDERROR) from the java program make it back to the browser?

  • How to run a java program via a batch file,which is called from a webserver

    REM This batch file runs the Spider with the [-v] option.
    REM Lines 51 through 54 are simple DOS commands..they call the Spider
    REM The location of the Spider package is the important piece of information here...
    REM a batch file has access to the whole computer where the server resides.
    setlocal
    set JDK=C:\j2sdk1.4.1_01
    set PATH=%JDK%\bin;.;%PATH%
    CLASSPATH=%JDK%\jre\lib\*.jar;.;%JDK%\jre\lib\ext\*.jar;.;%CLASSPATH%
    java spiderpackage.EntryPoint -v
    endlocal
    I am trying to call this batch file from my web server but I am unable to capture the result.I tried to append the result to a file but It writes nothing to it.What should I do to proceed?

    I am not entirely sure if this is analogous, but I once wrote a PHP script uner Linux that invokes java and returning the output. I found that it was necessary to pipe the output of the Java program from stderr to stdout in order to properly receive it in PHP. The command would be as follows:
    java spiderpackage.EntryPoint -v 2>&1
    The pipe syntax (2>&1) should be exactly the same under MSDOS. So add that, and then continue as normal (appending it to a file or whatever else you want to do). Hope that helps

  • Windows task scheduler to run batch file running a java program

    I have created a batch file that runs my SendEmail program. The batch file is in the same directory as the java program. The only line in the batch file is
    java SendEmail
    This works fine from the command prompt but from the windows task scheduler it fails to function. Any help on how I can overcome this problem will be greatly appreciated.
    THanks

    Looks okay. The classpath is set, too, I guess? Btw, try running javaw instead of java. I may err, but I think it can show a console, so you can watch any error 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.

  • Creating a batch file to launch a program with user input variable.

    I am trying to create a batch file that allows a user to be prompted to put in a value and then launches the program associated and that particular file. In this case the files are CIF files that pull up saved data inputs. Below is my script so far and what I am trying to accomplish is the user presses 1 and hits enter and is prompted to enter the CIF file name, then the application launches and the paramaters open the selected CIF file.ECHO OFF
    CLS
    :MENU
    ECHO.
    ECHO ...............................................
    ECHO PRESS 1, 2, for your task or 3 to EXIT
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open Cape Pack CIF FIle
    ECHO 2 - Open Calculator
    ECHO 3 - EXIT
    ECHO.
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO Cape Pack
    IF %M%==2 GOTO CALC
    IF %M%==3 GOTO EOF
    :Cape Pack
    cd %windir%\Program Files (x86)\cape211\...
    This topic first appeared in the Spiceworks Community

    I am trying to create a batch file that allows a user to be prompted to put in a value and then launches the program associated and that particular file. In this case the files are CIF files that pull up saved data inputs. Below is my script so far and what I am trying to accomplish is the user presses 1 and hits enter and is prompted to enter the CIF file name, then the application launches and the paramaters open the selected CIF file.ECHO OFF
    CLS
    :MENU
    ECHO.
    ECHO ...............................................
    ECHO PRESS 1, 2, for your task or 3 to EXIT
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open Cape Pack CIF FIle
    ECHO 2 - Open Calculator
    ECHO 3 - EXIT
    ECHO.
    SET /P M=Type 1, 2, or 3 then press ENTER:
    IF %M%==1 GOTO Cape Pack
    IF %M%==2 GOTO CALC
    IF %M%==3 GOTO EOF
    :Cape Pack
    cd %windir%\Program Files (x86)\cape211\...
    This topic first appeared in the Spiceworks Community

  • Please help how to run System commands and batch files thru java program

    Sir,
    I want execute OS commands like dir,cls..etc and batch files,.exe filesthru java program.
    I have tried it sofar thru Runtime.getRuntime().exec("execute.bat");
    but it is not working.anybody knows about how to run the system commands thru java please give me the code.
    thank you,
    regards,
    j.mouli

    I've seen other posts with this questions, and answers. Unfortunately I didn't pay much attention. But the basic idea is to exec command.com and pass the specifc batch or command as an argument.

  • How to invoke the .bat(batch file ) from the java program

    i want to run some commands when i run one java program.
    I wrote those dos commands on the batch file and i want to include the bat file in the java program so that i can execute the bat file when i run the java program.
    tell me the way that i can run my bat file inside the java program.

    i tried this :
    a .bat file named test.bat, with this code : copy test.bat test2.bat
    a java class, Test.class, in the same directory
    public class Test {
         public static void main(String[] args) {
              try {           
                   Runtime rt = Runtime.getRuntime();
                   Process proc = rt.exec("cmd /c test.bat");
                   proc.waitFor();
                   int exitVal = proc.exitValue();
                   System.out.println("Process exitValue: " + exitVal);
              catch (Throwable t) {
                   t.printStackTrace();
    }

  • Making batch files to execute java programs in windows

    In my comp sci class, i found a folder with a batch file, an application file, and all class files necessary to run the program.
    how do i take my java program and make a batch file to run it (in dos) like that program in my comp sci class?

    Okay, here are your steps for running a java program from a batch file:
    1) Open Notepad
    2) type the following:
    set JAVA_HOME=C:\j2sdk1.4.0_03 // specify the path where your jdk is
    set path=%JAVA_HOME%\bin;.
    javac {-classpath .;one.jar;two.jar} YourProgramName.java
    java {-classpath .;one.jar;two.jar} YourProgramName
    The stuffs inside {} is not necessary if your program doesnt use any .jar files.
    3) Save the notepad as mybatch.bat in the directory where your Java program is located.
    4) In the DOS prompt, go to the directory where you have .java and .bat files
    (use 'cd' command)
    5) Now, type mybatch.bat and press ENTER.
    Thatz it. Your program runs like a charm now!!!
    (provided the compiler is pleased with ur code :)
    -- layman's pal

  • Adobe AIR - java is not recognized as an internal or external command operable program or batch file

    Hi,
    I've done everything as in the HelloWorld tutorial but when i run this cmd: adt
    it gives me the following error java is not recognized as an internal or external command operable program or batch file
    I've installed the AIR runtime v2
    Downloaded the SDK v2 and placed in D:\Www\air
    Modified the path variable with D:\Www\air\bin
    And then i open a new cmd console and simply write adt and gives me that error.
    I've updated to the latest version of Java, but it did solve it.
    But what's strage it's that the adl command work, but i'm not being able to pack my application.
    I'm running Windows 7 on x64 platform.
    Sincerely,
    Alex

    I think the 32bit version will work, but it sounds like you'll need to add the Java bin folder to your system path.  This is usually done by the Java installer, so I'm not sure what failed, did you run the installer located at www.java.com?  Either way, this document might be able to help you out:
    How do I set or change the PATH system variable
    Add the Java "bin" folder to your path, you might also need to add the CLASSPATH.
    Please let me know how it goes!

  • Cannot call 2 batch files .. executes only one batch and the program exists

    Hello,
    I am trying to call 2 batch files from the program . due to some reason only first batch file is called and second one is not called . any idea why it is happening.
    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.io.*;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class MyBatchFiles {
          public static void main (String args[]) throws Exception {
        StringBuffer output  = new StringBuffer();
        StringBuffer error = new StringBuffer();
        try {
                   //call batch files
                   Process Process=Runtime.getRuntime().exec("cmd /c start F:\\batch1.bat"); // Batch File Path
                       Process.waitFor();
                      BufferedReader inputStreamReader  = new BufferedReader(new InputStreamReader(cubeBuildProcess.getInputStream()));
                   BufferedReader errStreamReader  = new BufferedReader(new InputStreamReader(cubeBuildProcess.getErrorStream()));
                   output = new StringBuffer();
                   error = new StringBuffer();
                   for(String ProcessLine;(ProcessLine=inputStreamReader.readLine())!=null;)
                             output.append(ProcessLine);
                   for(String ProcessLine;(ProcessLine=errStreamReader.readLine())!=null;)
                             error.append(ProcessLine);
                   System.out.println("transformer output = " + output);
                   System.out.println("transformer error = " + error);
                   if (error!=null){
                             //calling batch2
                             Process Process2=Runtime.getRuntime().exec("cmd /c start F:\\batch2.bat"); // Batch File Path
                             Process2.waitFor();
                             BufferedReader Process_inputStreamReader  = new BufferedReader(new InputStreamReader(Process2.getInputStream()));
                             BufferedReader Process_errStreamReader  = new BufferedReader(new InputStreamReader(Process2.getErrorStream()));
                             for(String ProcessLine1;(ProcessLine1=Process_inputStreamReader.readLine())!=null;)
                                       output.append(ProcessLine1);
                             for(String ProcessLine1;(ProcessLine1=Process_errStreamReader.readLine())!=null;)
                                       error.append(cubeCopyProcessLine);
           }catch(Exception e) {
                  e.printStackTrace();
    }Thanks
    Mike

    It's your if statement. Why do you think it could be happening?

  • (SCM-APO ) - LiveCache error - tp error : operable program or batch file

    Dear Gurus,
    On one our APO systems, I cannot administer liveCache, using database connection LCA, LDA, or LEA. Using LCA as an example, with
    tcode LC10> liveCache Monitoring, I get this error:
    Name and Server :    QDP - ntsapsc001
    DBMRFC Function  : DBM_CONNECT
    Error : DBM Error
    Return Code : -11
    Error Message : tp error: operable program or batch file
    Using tcode /sapapo/om13, I get these results:
    liveCache connection to LCA      (GREEN)
    liveCache Stat. from LCA     ???    (RED)
    liveCache Initialization          001   (RED)
    Using tcode SE38> program RSLVCINIT > database connection LCA, I get this error:
    Error in DBMSERVER or DBMCLI
    What is wrong ?
    Thanks and regards,
    cjperk90

    In the syslog (tcode sm21), and on the NT server's syslog,
    I also get these errors:
    Database error -10709 at CON
    > Connection failed (RTE:database not running)
    Please advise.
    Thanks,
    cjperk90

Maybe you are looking for

  • Formula that looks at two different fields

    Hi, I posted this request for help not too long ago but didn't clearly explain what is needed.  When text is entered into a field (currentgoal1, currentgoal2 - each equaling 1), the formula needs to look at a different field (point1, point 2 - each h

  • Problem with quotation marks in Reports

    Hello, in Reports i have a text item, and i wrote a text „test" (this is copy/paste from a Word Document). At runtime i see ?text? Why those quotation marks does not appear correctly at runtime? It's about Reports Developer 2000. Please help if you c

  • How to access the MS access database through network

    Hi friends, I want to read,write and some database functions through network.I hope it is possible. Steps i followed for network access 1. I created a database named magal using  DB.create_mdb_link.vi in local pc 1. 2. I used DB Tools open connection

  • Creating quick links from overview in ESS

    Hi all,        how do i create a quick link in the overview menu linking to leave request from the WORKING TIME category? Thanks!

  • Retrieving PageID in Plumtree v4.5 SP2

    Note: This post applies to portal version 4.5 SP2 - upgrading is not a solution. I have a community with multiple pages. Each page contains an instance of the same gadget, and the gadget allows an admin to set Community-Gadget preferences. The proble