How to execute a .cmd file in java

I can execute an .bat or a .exe file.
But .cmd file is not getting executed in java.
Pls tell me how to do this

You can solve this problem by using the following method:
  private String[] getEnvironmentVariables() {
    final Map<String, String> env = System.getenv();
    final String[] result = new String[env.size()];
    final StringBuilder buf = new StringBuilder(100);
    int i = 0;
    for (Entry<String, String> e : env.entrySet()) {
      buf.setLength(0);
      result[i++] = buf.append(e.getKey()).append('=').append(e.getValue()).toString();
    return result;
  }You just have to execute your command like this now:
Runtime.exec("cmd /C " + getMyCommand(), getEnvironmentVariables(), getMyExecDirectory());The reason why "start" didn't work is, that it is a command executed in a shell, thus you need "cmd /C" to execute start in the cmd. So you can do this as well:
Runtime.exec("cmd /C start " + getMyCommand(), getEnvironmentVariables(), getMyExecDirectory());If you want to to execute "myCommand" in an own process.
Kind Regards,
Stefan Schubert

Similar Messages

  • How to execute SQOOP.cmd file from SSIS execute process task?

    How to execute SQOOP.cmd file from SSIS execute process task?
    What is argument need to be prefixed in the arguments property
    Executable: C:\Hadoop\sqoop-1.4.2\bin\sqoop.cmd
    Arguments: /C "sqoop import -connect "jdbc:sqlserver://mysystem:1433;database=test;username=myuser;password=mypwd;" -table emp  -target-dir /user/emptable -m 1"
    Thanks
    Durga

    Hi Durga,
    SQOOP.cmd must be no different than any other .cmd/.bat I assume, your question is more about setting the parameters up I guessed.
    So unless the arguments change dynamically you can call SQOOP.cmd from yet another .cmd that has its parameters passed to it and the former you simply set to run without the arguments in Execute Process Task.
    Otherwise, in general to set a bat/cmd file to run in SSIS one needs to do this:
    PS: It is a good idea to populate the working directory with the path to the .cmd
    Arthur My Blog

  • How to execute a .cmd file in Solaris server

    Hi all , How to execute a .cmd file in Solaris server.
    Thanks.

    Basically you should have the execution flag associated to this cmd file. Check first if this file is builded for Solaris and then you can add the "x" flag to the file permissions.
    You can too run it like this, but you should also check if this file is Solaris compatible : # sh <path/cmd file>

  • How to execute a .exe file in java(Jsp) without using a process ???

    Hi All ,
    How to execute a .exe file in Jsp without using a process ??? ...
    Is it Possiable ????

    itsdhanasaraa wrote:
    But as this a web application ... By using Runtime i'm getting some probs ..
    Let me guess, you want your web application to run a program on the client and to your surprise that's not working?
    Ain't gonna happen.
    its taking more time to execute .... that's y is there any other option to execute .exe file other than Runtime.getRuntime().exec("filename");Write proper English and you may be taken more seriously.
    1) it's not "taking more time to execute", whatever that's supposed to mean.
    2) there's no other way to execute something. Not that you should every use even that way anyway
    3) whenever you start thinking of executing external programs from Java, start thinking of not using Java in the first place.

  • How to execute an external file in java ?

    How to execute an external file , where the file name of the external file is in unicode character. (i,e, may be in Chinese character).

    Process p = Runtime.getRuntime().exec("/path/to/executable");
    This is write. But my question is while the file name is in Chinese font.
    It means while a file name is in Chinese font or any unicode character that are supported by window but not in the command prompt. In this case the file name is changed to ??????.doc like.
    So, while trying to execute this it shows File not found message.

  • How to Execute a cmd file in my ps script?

    Hi All,
    I have a cmd file which calls an msi and passes paramters. I am calling this deploy.cmd file from a powershell script. How can i achive this? I am trying the following but it does not work.
    Invoke-Command {&
    "D:\deploy\ABC_Installed\ETRM
    (QA)_1.0.7.2\en-us\ABC_QA.cmd"}
    It tends to just print the contents of the cmd but does not execute it. So, is it possible to execute a cmd
    from with in PS, if so, how?
    Walk_Man... Share the joy... 'Propose as Answer' if the post helps.

    Hey mate,
    I get the following error message when i run this,
    $proc
    =
    [diagnostics.process]::Start("cmd.exe",
    "/c"D:\deploy\ABC_Installed\ETRM
    (QA)_1.0.7.2\en-us\ABC_QA.cmd")
    Missing ')' in method call.
    At :line:12 char:53
    + $proc = [diagnostics.process]::Start("cmd.exe", "/c"D <<<< :\deploy\ABC_Installed\ETRM (QA)_1.0.7.2\en-us\ABC_QA.cmd")
    Walk_Man... Share the joy... 'Propose as Answer' if the post helps.

  • How to execute any cmd command from java application?

    Hi all,
    How to execute or call any command of cmd from java application??
    Is there any method to do so??
    Or, is it possible to do it using Runtime.exec() ??? And if so, how to use it, please explain with ab example...
    I'll highly appreciate....
    Thank you.

    If google would be the best option, then I would not be on Sun's forums and I would not have asked experts like you, sir !! :-)
    Neway, I got the solution from PhHein !!
    Good link indeed..
    Cheers..

  • Execute a .cmd file from Java program on tomcat which is running as service

    I have a .cmd/,.bat file which has to be executed depending on the parameters I pass through JSP page hosted on a tomcat instance.
    Tomcat is running as a windows service.
    I need my .cmd file to be opened in a cmd or DOS box so that user can interact with the dos window, which is mandatory.
    When I run the program from eclipse it works well but on the tomcat , the server side the output of the command is written to my log file and it gives no chance for users to monitor and interact with the command.
    Should I do something with log4j settings or is due to tomcat running as service?
    Please help.
    thanks

    thanks for the responses.
    But the design is like that.Then it needs to be fixed.
    I think this is why there's a shortage of people to write computer applications, they are all wasting their time struggling to implement bad designs.
    its a scheduler to be run on tomcat where we interact
    with the running batch files and can't be changed.A scheduler in Tomcat? Why run a scheduler in an application server?
    any help is appreciated.
    I tried to execute the batch file from servlet it
    runs , but i see the cmd.exe in windows task manager
    and need a dos box open.
    Any ideas?If you need a DOS box open then you need a signed-on user. So here's my patch:
    1. Write an application that listens for messages from this Tomcat scheduler, and when it receives those messages it does something in a DOS box.
    2. Have somebody sign on to the server and run that application.
    3. Change the Tomcat scheduler to send messages to that application instead of trying to actually run it.
    It's up to you what kind of messages you should send. There are plenty of choices.

  • How to execute a EXE file in java?

    Hi!All
    as title
    Can I use Java to call any EXE file.
    I need your help!!

    ~~~Use This~~~~
    try{
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(
    "C:/Program
    Program Files/InternetExplorer/IEXPLORE.EXE
    http://192.168.1.101/index.jsp");
    why has private IP there?
    } catch (IOException e) {
    e.printStackTrace();
    }I and he want to a file which could run as .exe file.

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

  • Execute a .cmd file?

    Is it possible to execute a .cmd file using a button or some other means?
    We have a .cmd file that executes sqlloader to reload a table with new data from a flat file and would like to run this .cmd file from an apex application.
    TIA.

    Hi Pixie,
    About all I know about java is that I hate it. However, this is actually one of those rare java apps that is actually useful and easy.
    Just follows the steps on that web page, with a couple of small edits, and your done. You can then create the button i n your Apex app which performs the process and executes the shell script.
    The only gotcha I've had with this was when I upgraded to ver 11 (maybe 10?). Oracle had changed the security for the database, so I needed to change how Oracle started, was owned, etc. so that I could define the proper environment variables for when the shell script executed. I did all the steps as the owner of the schema my Apex app uses, so all of the internal permissions were correct.
    Bill Ferguson

  • How to call a .bat file from java code?

    How to call a .bat file from java code? and how can i pass parameters to that .bat file?
    Thanks in advance

    thanks for ur reply
    but still i am getting the same error.
    I am trying to run a .bat file of together tool, my code looks like below
    import java.lang.Runtime;
    import java.lang.Process;
    import java.io.File;
    class SysCall{
         public static void main(String args[]){
              String cmd="D://Borland//Together6.2//bin//Together.bat -script:com.togethersoft.modules.qa.QA -metrics out:D://MySamples//Metrics// -fmt:html D://Borland//Together6.2//samples//java//CashSales//CashSales.tpr";
              //String path="D://Borland//Together6.2//bin//Together.bat ";
              Runtime r= Runtime.getRuntime(); //Declare the system call
              try{
                   System.out.println("Before batch is called");
                   Process p=r.exec(cmd);
                   System.out.println(" Exit value =" + p.exitValue());
                   System.out.println("After batch is called");
              /*can produce errors which must be caught*/
              catch(Exception e) {
                   e.printStackTrace();
                   System.out.println (e.toString());
    I am getting the below exception
    Before batch is called
    java.lang.IllegalThreadStateException: process has not exited
    at java.lang.Win32Process.exitValue(Native Method)
    at SysCall.main(SysCall.java:17)
    java.lang.IllegalThreadStateException: process has not exited

  • Executing mysql script file in java

    Hello,
    I am working on a research project that involves the creation of anywhere from 50 to 200 MySQL databases at a time. So far I am able to execute CREATE DATABASE commands successfully using the Statement.execute() command.
    So onto my problem.....
    I have the script files needed to create the tables for each database, but I cannot figure out how to run them through java. I am trying to execute a SOURCE /file_location/ statement, but I am getting "you have an error in your mysql syntax....".
    Does anyone know if there is a way to execute a script file from java?

    You mean to say that you have websphear application server (WAS) installed on Windows NT machine. Can you please share how you have deployed your application on WAS. The problem might be just because of configuration. What error/exception you are getting when you run application in WAS.

  • How to execute one .exe file with as3 in air ?

    Hi
    How to execute one .exe file with as3 in air?
    I want do this work without fscommand .
    plize help me .

    itsdhanasaraa wrote:
    But as this a web application ... By using Runtime i'm getting some probs ..
    Let me guess, you want your web application to run a program on the client and to your surprise that's not working?
    Ain't gonna happen.
    its taking more time to execute .... that's y is there any other option to execute .exe file other than Runtime.getRuntime().exec("filename");Write proper English and you may be taken more seriously.
    1) it's not "taking more time to execute", whatever that's supposed to mean.
    2) there's no other way to execute something. Not that you should every use even that way anyway
    3) whenever you start thinking of executing external programs from Java, start thinking of not using Java in the first place.

  • How to execute a SELECT statement  in java??

    Hello !!
    In my java program , i need to delete a record of number X, so
    i accept the number X from the keyboard
    Then before deleting it
    i want the program to show me the name, age of the record which has the number X
    How to do this

    hello kylas
    actually i didnt get why this program example?? wats
    its executing ??? Look at reply #3 in your other thread:
    http://forum.java.sun.com/thread.jspa?threadID=713289&messageID=4126346
    Notice the similarity? You've now asked "How to delete a record in Java" and "how to execute a select statement in java". You may have noticed that, aside from the SQL and the call to execute and executeUpdate (for delete) it's the same code. This is because you don't so much do these things in Java, you do them in SQL. The Java code simply asks the Driver to execute whatever SQL you write. So, I really hope your next post isn't "how do I execute an UPDATE statement in Java".
    Good Luck
    Lee

Maybe you are looking for