Running batch files thraugh java by passing parameters

Hi
I want to run a batch file by passing some parameters.
Eg: copy.bat "D:\live\hoe.txt" "D:\test"
while doing this from command prompt its working and i have written some java code for running this batch file.
String live="D:\\live\\how.txt";
String test="D:\\test";
String bat="D:\\copy.bat";
String[] command = new String[3];
command[0] = bat;
command[1] = live;
command[2] = test;
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
but this time its not copying the file;
Please help.

Just another cross poster.
[http://www.java-forums.org/new-java/15005-running-batch-files-thraugh-java-passing-parameters.html]
db

Similar Messages

  • I need to call a batch file from java and pass arguments to that Batch file

    Hi,
    I need to call a batch file from java and pass arguments to that Batch file.
    For example say: The batch file(test.bat) contains this command: mkdir
    I need to pass the name of the directory to the batch file as an argument from My Java program.
    Runtime.getRuntime().exec("cmd /c start test.bat");
    How to pass argument to the .bat file from Java now ?
    regards,
    Krish
    Edited by: Krish4Java on Oct 17, 2007 2:47 PM

    Hi Turing,
    I am able to pass the argument directly but unable to pass as a String.
    For example:
    Runtime.getRuntime().exec("cmd /c start test.bat sample ");
    When I pass it as a value sample, I am able to receive this value sample in the batch file. Do you know how to pass a String ?
    String s1="sample";
    Runtime.getRuntime().exec("cmd /c start test.bat s1 ");
    s1 gets passed here instead of value sample to the batch file.
    Pls let me know if you have a solution.
    Thanks,
    Krish

  • Error running batch files from java source file???

    Dear Friends,
    hi,
    this is with response to a doubt i had earlier ,
    i want to run batch files from the java source file ,i tried using this code (here batrun is the batch file name that contains commands to run other java files)
    try
    String [] command = {"c:\\vishal\\finalmain\\batrun"};
    Runtime.getRuntime().exec(command);
    catch(Exception e)
    but i got the following error.
    java.io.IOException: CreateProcess: gnagarrun error= 2
    plz. help me, i tried all combination w/o success,
    in anticipation(if possible give the code after testing)
    Vishal.

    hello there,
    i solved the prob. by using
    cmd /c start filename ,but i need to pass parameters ie
    cmd /c start java "c:/vishal/runfile a b" where a and b are the parameters. but it is not accepting this in Runtime.getRuntime.exec(),
    any solutions ?????????
    regards,
    Vishal

  • Run batch files through java code

    Hi All
    I need to run a batch file using java code . I am not getting any error, but also no output. Can someone let me know the problem with my code.The code i am using is
    import java.io.*;
    public class batchtest {
         public static void main(String[] args) {
              Runtime r = Runtime.getRuntime();
         Process p= null;
         String line;
              try{
              p = r.exec("cmd /c testBatch.bat");
              BufferedReader input = new BufferedReader
              (new InputStreamReader(p.getInputStream()));
         while ((line = input.readLine()) != null) {
         System.out.println(line);
         input.close();
              System.out.println("running...");
              }catch(Exception e)
                   e.printStackTrace();
    }

    Maybe this helps:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Running batch files using java

    Hi,
    How can i run two batch files in a specified interval(Eg. 1 hour) using java.
    Thanks

    Hi,
    Learn to Use the API.
    import java.io.IOException;
    import java.util.Timer;
    import java.util.TimerTask;
    public class RunBat {
         static void runBatchFiles(String[] batchFiles)
              final String[] files = batchFiles;
              Timer timer = new Timer();
              TimerTask task = new TimerTask()          {               
                   Runtime r = Runtime.getRuntime();               
                   public void run() {
                        for(String command : files)
                             try {
                                  r.exec(command);
                             } catch (IOException e) {                              
                                  e.printStackTrace();
              timer.schedule(task, 1000, 1000*60*60);
         public static void main(String[] args) {
              runBatchFiles(new String[]{"cmd /C start f:/one.bat","cmd /C start f:/two.bat"});
    }Thanks

  • Running batch files  from Java using exec method

    Hi,
    I want to run a batch file from my Java program like this:
    try {
    Process proc = Runtime.getRuntime().exec("C:\\Refresh.bat");
    catch (Exception e) {
    MessageBox.show(e.getMessage());
    Refresh.bat file contains two commands.
    First one unzips certain zip file.
    Second one refreshes a SQL Server database using osql utility.
    Problem is that when program is run it executes only the first command and hangs on the second one.
    Please help.
    TIA
    Ravinder

    From the FAQ:
    2. How do you launch an external program on a Microsoft Windows platform from a program developed on the Java [tm] programming language?
    The following will launch notepad in Microsoft Windows NT:
    Runtime.getRuntime().exec("cmd /c notepad.exe");
    To launch a program in Microsoft Windows 95/98 use:
    Runtime.getRuntime().exec("c:\\windows\\notepad.exe");
    The Runtime class allows interaction between a program and its environment. The first string command instructs the command line interpretor, cmd to open up the calculator application.
    The exec() methods do not use a shell; any arguments must have the full pathname to the shell as well as the command itself.
    For example, to run a shell on the UNIX� platform, type:
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("/usr/bin/sh -c date");
    To run a batch file under Microsoft Windows 95/98:
    Process p = rt.exec("command.com /c c:\\mydir\\myfile.bat");
    To run a batch file under Microsoft Windows NT:
    Process p = rt.exec("cmd /c c:\\mydir\\myfile.bat");
    where 'cmd' and 'command.com' are the command-line interpreters for Microsoft Windows machines.
    The Runtime.exec() methods might not work effectively for some processes on certain platforms. Special care should be taken with native windowing, daemon, WIN16/DOS process or some shell scripts.
    regards,
    jarshe

  • 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.

  • Running a batch file in Java. Urgent!!

    I want to run a batch file present at a particular location in Java.Although I am running two codes suggested by many people, i am still not able to run the batch file. My codes run as below :-
    CODE 1
    Process P;
    Runtime rt = Runtime.getRuntime();
    P = rt.exec("C:\\test.bat");
    CODE 2
    Process P;
    Runtime rt = Runtime.getRuntime();
    P = rt.exec("C:\\WINNT\\System32\\cmd.exe C:\\test.bat");
    I have tried both these codes but without success. But I have observed that in the Task Manager, whenever i run the above codes in a servlet, a "CMD.EXE" is being created in the "Processes" Bar each time but the batch file neither opens up nor it runs.
    Please suggest me some method to run this batch file in Java.
    Or if u can suggest the reason behind the above observation and any corrections i can make to run the batch file. Thanks in advance.
    Ankit

    i want to get more than one command prompt while running the program
    am giving the problem
    import java.io.*;
    class CmdDemo
         CmdDemo(String ls) {
              try
              Process p;
              Runtime rt=Runtime.getRuntime();
              p=rt.exec(ls);
              catch(IOException ioe)
                   System.out.println(ioe);
         void showcmd(){
    new CmdDemo("cmd");
         public static void main(String args[])
              new CmdDemo("cmd");
              new CmdDemo("C:/WINDOWS/System32/cmd.exe");
              new CmdDemo("cmd");
              new CmdDemo("regedit");
              new CmdDemo("regedit");
              new CmdDemo("notepad");
              new CmdDemo("notepad");
              //CmdDemo cd=new CmdDemo("cmd");
              //cd.showcmd();
    here am getting two notepads ,two registry editors
    but only one command window
    i would be very greateful if u could help me
    plz reply to my id [email protected] too

  • HOw to create a Batch file for java application and whats the use of this ?

    HI,
    How to create a Batch file for java application ?
    And whats the use of creating batch file ?
    Thanks in advance

    First of all, you're OT.
    Second, you can find this everywhere in the net.
    If you got a manifest declaring main class (an classpath if needed), just create a file named whatever.bat, within same directory of jar file, containing:
    javaw -jar ./WhateverTheNameOfYourJarIs.jar %*By the way, assuming a Windows OS, you can just double click the jar file (no batch is needed).
    Otherwise use:
    javaw -cp listOfJarsAndDirectoriesSeparedBySemiColon country/company/application/package/className %*Where 'country/company/application/package/' just stands for a package path using '/' as separator instead of '.'
    Don't specify the .class extension.
    Javaw only works on Windows (you asked for batch, I assumed .BAT, no .sh), in Linux please use java.exe (path may be needed, Windows doesn't need it 'cause java's executables are copied to system32 folder in order to be always available, see PATH environment variable if you don't know what I'm talking about) and use ':' as classpath (cp) separator.
    The '%***' tail is there in order to pass all parameters, it only works on Windows, refer to your shell docs for other OSs (something like $* may work).
    This way you have a command you can call to launch your code (instead of opening NetBeans just to see your app working). You could schedule tasks on it or just call it in any command prompt (hope you know what it is 'cause there have been people in this very same forum with no clue about it, if not just hold the 'Windows button' and press 'R', then type 'cmd' and run it).
    Finally add dukes and give 'hem away.
    Bye.

  • Executing batch file from Java stored procedure hang

    Dears,
    I'm using the following code to execute batch file from Java Stored procedure, which is working fine from Java IDE JDeveloper 10.1.3.4.
    public static String runFile(String drive)
    String result = "";
    String content = "echo off\n" + "vol " + drive + ": | find /i \"Serial Number is\"";
    try {
    File directory = new File(drive + ":");
    File file = File.createTempFile("bb1", ".bat", directory);
    file.deleteOnExit();
    FileWriter fw = new java.io.FileWriter(file);
    fw.write(content);
    fw.close();
    // The next line is the command causing the problem
    Process p = Runtime.getRuntime().exec("cmd.exe /c " + file.getPath());
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = input.readLine()) != null)
    result += line;
    input.close();
    file.delete();
    result = result.substring( result.lastIndexOf( ' ' )).trim();
    } catch (Exception e) {
    e.printStackTrace();
    result = e.getClass().getName() + " : " + e.getMessage();
    return result;
    The above code is used in getting the volume of a drive on windows, something like "80EC-C230"
    I gave the SYSTEM schema the required privilege to execute the code.
    EXEC DBMS_JAVA.grant_permission('SYSTEM', 'java.io.FilePermission', '<<ALL FILES>>', 'read ,write, execute, delete');
    EXEC DBMS_JAVA.grant_permission('SYSTEM', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
    EXEC DBMS_JAVA.grant_permission('SYSTEM', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
    GRANT JAVAUSERPRIV TO SYSTEM;
    I have used the following to load the class in Oracle 9ir2 DB:
    loadjava -u [system/******@orcl|mailto:system/******@orcl] -v -resolve C:\Server\src\net\dev\Util.java
    CREATE FUNCTION A1(drive IN VARCHAR2) RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'net.dev.Util.a1(java.lang.String) return java.lang.String';
    variable serial1 varchar2(1000);
    call A1( 'C' ) into :serial1;
    The problem that it hangs when I execute the call to the function (I have indicated the line causing the problem in a comment in the code).
    I have seen similar problems on other forums, but no solution posted
    [http://oracle.ittoolbox.com/groups/technical-functional/oracle-jdeveloper-l/run-an-exe-file-using-oracle-database-trigger-1567662]
    I have posted this in JDeveloper forum ([t-853821]) but suggested to post for forum in DB.
    Can anyne help?

    Dear Peter,
    You are totally right, I got this as mistake copy paste. I'm just having a Java utility for running external files outside Oracle DB, this is the method runFile()
    I'm passing it the content of script and names of file to be created on the fly and executed then deleted, sorry for the mistake in creating caller function.
    The main point, how I claim that the line in code where creating external process is the problem. I have tried the code with commenting this line and it was working ok, I made this to make sure of the permission required that I need to give to the schema passing security permission problems.
    The function script is running perfect if I'm executing vbs script outside Oracle using something like "cscript //NoLogo aaa1.vbs", but when I use the command line the call just never returns to me "cmd.exe /c bb1.bat".
    where content of bb1.bat as follows:
    echo off
    vol C: | find /i "Serial Number is"
    The above batch file just get the serial number of hard drive assigned when windows formatted HD.
    Same code runs outside Oracle just fine, but inside Oracle doesn't return if I exectued the following:
    variable serial1 varchar2(1000);
    call A1( 'C' ) into :serial1;
    Never returns
    Thanks for tracing teh issue to that details ;) hope you coul help.

  • Running BAT file from Java

    how would i go about calling a batch file in java?
    a button is clicked that says "RUN" and then I want to run a batch file

    Hi !!!
    Im new to this forums but I think I can help you
    You can execute a separate process by using the class Runtime. In this example I execute one batch file located on the same directory the Java App is in.
    class Test
         public static void main(String a[])
              System.out.println("Executing Batch File");
              try{
              Runtime.getRuntime().exec("Runme.bat");
              catch(Exception ex){}
    }I hope that could help
    see
    how would i go about calling a batch file in java?
    a button is clicked that says "RUN" and then I want to
    run a batch file

  • How to create a Batch file for java application  ?

    HI,
    How to create a Batch file for java application ?
    And whats the use of creating batch file ?
    Thanks in advance

    [http://static.springsource.org/spring-batch/]
    Assuming you want to develop a batch application rather than actually just create a .bat file ..

  • How to run ear file in java application server

    i want how to run .ear file in java application server
    1. i m created ear file
    2. i m created jar file (bean,home,remote)
    3.i m created war file(in the form of jsp)
    but till now i couldnt run ear file
    how to run
    please hel me

    You must create :
    1.Jar file
    2.War file
    And then put them into an ear file
    Exemple : myapp.ear contains
    myappEJB.jar
    myappWEB.war
    META-INF/application.xml
    and application.xml looks like this :
    <application xmlns="http://java.sun.com/xml/ns/j2ee" version="1.4"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com /xml/ns/j2ee
                            http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
        <display-name>myapp</display-name>
        <description>Demo application</description>
        <module>
            <ejb>myappEJB.jar</ejb>
        </module>
        <module>
          <web>
             <web-uri>myappWAR.war</web-uri>
             <context-root>/myapp</context-root>
          </web>
        </module>
    </application>Good luck

  • Strange......problem with batch file in java

    hi,
    my problem is that i have created one bat file and i want to run that file in java but it is not running properly. the problem is .......
    i have created one bat file named "file1.bat" & content of this file is as follows :-
    [ ag > b.txt]
    (and content of file "ag" is "aaaaaaa" )
    now i am trying to run this bat file in java and want to read this b.txt in which i have redirected the content of file "ag".
    for that i have written the code like this:-file name is "runbf.java"
    import java.awt.*;
    import java.lang.Runtime;
    import java.io.*;
    public class DriveVol
    public static void main(String args[]) throws IOException
    int volumn;
    Runtime r = Runtime.getRuntime();
    r.exec("ss.bat");
    FileInputStream fp;
    fp = new FileInputStream("b.txt");
    flush();
    do
    volumn = fp.read();
    System.out.print((char)volumn);
    }while(volumn != -1);
    fp.close();
    when i am running this programme first it is not displaying the content of b.txt. but when u run this prog twice then only it 'll give the correct ans. what i mean is, i'll explain that stepwise.
    1. as per my prog. -- r.exec("aa.bat") -- this line 'll execute first. so content of ag 'll redirect to file "b.txt"
    2. now i am trying to read the file "b.txt" using FIleInputStream...
    3. but first time it is giving some garbbage value.when i give --c:jdk1.3\bin>java runbf
    4. but the very next moment when again i run that file - c:jdk1.3\bin>java runbf
    it 'll give the correct value.
    5. again when i change the content of file ag(suppose "bbbbbbb"). & try to run "runbf.java" like this-
    -- c:jdk1.3\bin>java runbf --
    6. then it 'll display the value "aaaaaa". now when again i run that file
    it 'll give the correct value "bbbbbb"
    7. so every time i have to run that "runbf.java" twice for printing the correct
    value of file "b.txt"
    so what i think is that, it is not refreshing the data of file "b.txt". after executing this line
    r.exec("aa.bat");
    so is there any method to refresh the data of file "b.txt"??????
    i think u 'll try to understand my problem...
    so please help me to get the ans.......
    thx.........

    pls can anyone hlp............

  • HOST IN 10g to run batch file on application server

    Hi,
    I am trying to deploy a form from 6i to 9i. the form use Host builtin to run batch file on machine. this works fine when i run form on client server environment. but when i run the same form after compiling in 9i and run on 3 tier model and try to run batch file from form it give me error that
    'ftp' is not recognized as an internal or external command,
    operable program or batch file.
    any suggestion?

    It could be because the Host session doesn't have any environment variables set i.e. there is no PATH variable, so it simply doesn't know where to look to find the 'ftp' executable. You could try supplying the explicit path of the ftp executable and see if that works. (This was a problem I had running Host commands on a Unix application server.)
    James

Maybe you are looking for

  • Locking the screen shortcut from a standard keyboard

    Hello, Have a question. Does anyone out there know what the shortcut to lock the screen is when using a (no mac) standard external keyboard? I know that Control+Option+Eject works on the Macbook keyboard, but I'm suing a standard Microsoft Keyboard.

  • Error Report: Songs are not audio, just digital noise

    spotify:album:7FQoSF1t0zNLt0PGeLKGEW Many tracks on this Album (M Hurd: Orchestral Music and Pop Cantatas) are full of hissing digital static.  Specifically the Hip Hip Horatio tracks.  It's the only time I've come across the problem on this service,

  • Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:

    Could anyone please explain me what is this error about Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer at timetablegen.MainFrame.jButton17ActionPerformed(MainFrame.java:1816) at timetablegen.MainFrame.access$21

  • Pan & Zoom plug-in experience

    Anyone have any experience or review of the Pan & Zoom plug-in from stagetools.com (moving pictures) versus lyric.com (pan zoom pro? There is a significant difference in price.

  • Finder suspect in frequently crashing MacBook Pro

    MacBook Pro mid 2010 with ML crashes very frequently that it is begining to be unusable. It used to crash in Lion, but after a clean install of ML, it crashes much more. When it crashes I get a black screen and immediately later i get a grey screen a