Running windows command through java code

Hello
i want to execute jar.exe through java code , i have written following piece of code , but it isn't working
ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"cmd.exe","/c","%java_home%\\bin\\jar.exe"});
          Process process = processBuilder.start();
          BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
          String line = inputReader.readLine();
          while(line != null){
               System.out.println(line);
               line = inputReader.readLine();
does anybody knows why
Regards
Edited by: Mayur Mitkari on Mar 5, 2013 10:19 PM
Edited by: Mayur Mitkari on Mar 5, 2013 10:20 PM
Edited by: Mayur Mitkari on Mar 5, 2013 10:20 PM

sorry for that , but the
Runtime runtime = Runtime.getRuntime();
          Process proc = runtime.exec(new String[]{"cmd.exe","/c","jar"});
          proc.waitFor();
int i = proc.exitValue();
this code was different from first one
and in case of Process if runtime .exec is succesful it is wainting for long time , in this case i want if the runtime.exec is succesful something should be returned
Regards

Similar Messages

  • How to run solaris commands through java code ....

    Hi,
    actually i want to run some solaris commands for zipping some files on Solaris OS...
    any idea how can i do that ?
    thanks

    public class TABLES
    public static void main( String[] args )
                      //database is connected
            try
                   Connection con = null;
                   Statement stmt = null;
                   String strShowTables = "";
                ResultSet resultSet = null;
                   // CBA Statistics period is m_lStatisticsPeriod minutes
                   con = DriverManager.getConnection( g_strRWURI, g_strRWUsername, g_strRWPassword );
                   stmt = con.createStatement();
               ResultSet rs = stmt.executeQuery("use db");
             resultSet = stmt.executeQuery(strShowTables);
        String tableName = "";
             while(resultSet.next()){
    tableName = resultSet.getString(1);
              System.out.println(tableName);
            break;
    String strCmd = "tar cvzf file.tar.gz var/lib/mysql/db/GROUPS.*";
    Process p= Runtime.getRuntime().exec(strCmd);
    System.out.println(strCmd);
        stmt.close();
                rs.close();
                resultSet.close();
                   con.close();
              catch ( Exception e )
                   System.out.println( ": Failed to create database connection (" + e.getMessage() + ")" );
                   e.printStackTrace();
              catch ( Throwable t )
                   System.out.println( " Throwable: " + t.getMessage() );
                   t.printStackTrace();
        }//end of main mehtod
    }//END OF CLASSi hava tried the above code... what the problem is
    when is run that command on shell >    tar cvzf file.tar.gz var/lib/mysql/db/GROUPS.*i works fine but in code even though it didn't give any error but the created "file.tar.gz" is empty...
    Edited by: aftab_ali on Apr 7, 2009 7:15 AM
    Edited by: aftab_ali on Apr 7, 2009 7:17 AM

  • Need to track windows processes through java code.

    Need to track windows processes through java code.
    Eg: I want to find out whether an exe file (wrun.exe) is running or stopped.
    Can I do it through java. If so can any one please tell me how to do it. That will be a great help.
    Thanks,
    Ramesh

    There are 2 options for things like this:
    1) Use Runtime.exec() to execute some command or application and parse the input from it.
    2) Write some native code and use JNI to call it.

  • How to find IP allocated to windows OS through java code

    Hi,
    I want to find no of IPs allocated to windows OS through java code. How i can do this.Please give me idea.
    any help will be appricated.
    Thanks in advance
    Ravi Adha

    You can use the InetAddress class in the java.net package to find all the IP addresses of your computer.

  • How to run a command prompt " command " through java code

    hi all,
    There is a command
    "java -jar selenium-server.jar -interactive"
    which i am running through command prompt after going to D:\MyFolder\Examples .
    i want to execute this command using java code .please help

    This has already been answered in your other two threads on this topic - http://forum.java.sun.com/thread.jspa?threadID=5221221&messageID=9898287#9898287 and http://forum.java.sun.com/thread.jspa?threadID=5221223&messageID=9898290#9898290.
    For some reason you don't want to read the reference that tells you exactly how to do what you want and how to avoid the pitfalls - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html .

  • Running a command from java code

    hi all,
    There is a command
    "java -jar selenium-server.jar -interactive"
    which i am running through command prompt after going to D:\MyFolder\Examples .
    i want to execute this command using java code .please help

    subratjyetki wrote:
    please answer in detail or if posible can u give the code for thisOnce more -
    The detail is given in the reference. Why don't you read it?
    I could give you the code but it will cost you �100 per hour.

  • Running cmd commands through java

    Hi,
    I am trying to run few dos commands through java using Runtime.exec("command") but I am getting following error
    commmand is cd D:\CodeMerge\A\
    java.io.IOException: CreateProcess: cd.. error=2
    at java.lang.Win32Process.create(Native Method)
    at java.lang.Win32Process.<init>(Unknown Source)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at createDirectory.main(createDirectory.java:24)
    Basically what I am looking for is this:
    1) My program is in D:\Amit\RM
    2) I want to get out to D:\ go to CodeMerge\A\.
    3) Create folder with curretn date as name of the folder
    Hence I am doing
    rTime.exec("cd../..");
    rTime.exec("cd D:\CodeMerge\A\");
    rTime.exec("mkdir "+date);
    Can anybody tell me why I am getting this error
    Jounty

    U can run either DOS Commands or UNIX Commands thru the following Java Program
    Written By BalaNagaraju Malisetti
    contact: [email protected]
    import java.io.*;
    public class RunBdiff
    public static String rununixcmd(String v_prev_path,String v_curr_path,String v_delta_path,String v_beg,String v_len)
    String s = null;
    FileOutputStream fos;
    DataOutputStream dos;
    try
    int v_int_beg = Integer.parseInt(v_beg);
    int v_int_len=Integer.parseInt(v_len);
    int v_int_end=(v_int_beg+v_int_len)-1;
    String v_pos1=String.valueOf((v_int_beg+2));
    String v_pos2=String.valueOf((v_int_end+2));
    String cmd1 = "bdiff ";
    String cmd2=v_prev_path;
    String cmd3=" ";
    String cmd4=v_curr_path;
    String cmd5="| grep '^>'";
    String cmd6="| cut -c";
    String cmd7=v_pos1+"-"+v_pos2;
    String cmd8="| sort ";
    String cmd9="| uniq ";
    String cmd=cmd1+cmd2+cmd3+cmd4+cmd5+cmd6+cmd7+cmd8+cmd9;
    String[] commands = {"/bin/csh","-c",cmd}; //in Unix
    //String[] commands = {"cmd.exe","-c",cmd}; //in Windows
    Process p = Runtime.getRuntime().exec(commands);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    // read the output from the command
    File file = new File(v_delta_path);
    fos = new FileOutputStream(file);
    dos=new DataOutputStream(fos);
    //Writing the output of bdiff command to Delta File
    while ((s = stdInput.readLine()) != null)
    dos.writeChars(s);
    //Return Error Message to Calling Procedure
    while ((s = stdError.readLine()) != null)
    return s;
    return s;
    catch (IOException e)
    return String.valueOf(e);
    public static void main(String args[])
    String msg=rununixcmd("/home/g_anil/MS_ADDRESS1.dat","/home/g_anil/MS_ADDRESS2.dat","/home/g_anil/MS_ADDRESS_DELTA.dat","1","9");
    **************************************************************************************************************************************

  • To run a command using java code

    hi all,
    There is a command
    "java -jar selenium-server.jar -interactive"
    which i am running through command prompt after going to D:\MyFolder\Examples .
    i want to execute this command using java code .please help

    subratjyetki wrote:
    please answer in detail or if posible can u give the code for thisThe detail is given in the reference. Why don't you read it?
    I could give you the code but it will cost you ?100 per hour.

  • Running a exe (java command) through Java Code

    Hi I have a program that runs on command line using java command.This program processes a text file that it takes as an argument.I have many files to process so I cannot give command for each individual file.
    Is it possible that I use Java command to run this program for many files in a loop?
    If yes HOW?

    It has many parameters but one parameter for input file. But I think this is not a problem, I can change it as you said.Now I have tried this code:
    Note:In this code I can change B.txt each time in a loop for new input file. (This is just for check)
    import java.io.*;
    public class Test3
         public static void main(String[] args)
              Runtime run = Runtime.getRuntime();
              try
    Process pp=run.exec("C:/Program Files/Java/jdk1.6.0/bin/java -mx300m -classpath  B.txt > sample-output.txt");
                   BufferedReader in =new BufferedReader(new InputStreamReader(pp.getErrorStream()));
                   String line;
                   while ((line = in.readLine()) != null)
                        System.out.println(line);
                   int exitVal = pp.waitFor();
                   System.out.println("Process exitValue: " + exitVal);
              catch (Exception e)
                   e.printStackTrace();
                   System.out.println(e.getMessage());
    } It compiles well but gives exception NoClassDefFound when I use java command to interpret it.Can u help?
    Edited by: kimskams on Jun 29, 2008 7:24 AM

  • Running Unix command through Java

    Hi
    I am trying to run the unix command "rf filename" through Java and it doesnt seem to work.
    Can anyone help in this case please
    String cm = "rm ";
    String delFile =  args[0];        // this path is /data/temp/filename.doc
    Process p = Runtime.getRuntime.exec(cmd +delFile);Also since i dont have access to delete the file through my login i always login as root for a few commands.
    Is there a way i can specify user name & password & then run the command?
    Please let me know
    Thanx

    Please discard the above msg i got a solution by just adding file.delete
    thanx

  • Running OpenSSL command through Java Program

    How do I execute openssl commands through a java program? Any packages or wrapper classes are there? Please help.
    Thanks.

    Hi!
    What do you mean execute commands? Like: "openssl x509 -in cert.pem -out certout.pem" ??
    In that case you can just try the following:
    import java.lang.Runtime;
    try {
    Runtime.getRuntime().exec("openssl x509 -in cert.pem -out certout.pem");
    }catch (Exception e) {
    e.printStackTrace();
    ........

  • 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

  • How to run windows program through java applications.

    pls help me!!!!! code RED!~

    C:\Documents and Settings\student\Desktop\Running.java:12: cannot resolve symbol
    symbol : variable JMStudio
    location: class Running
    String path="C:/Program Files/MF2.1.1/bin"+JMStudio;
    ^
    1 error
    this error is generated
    any idea on how to solve it?

  • Execute java commands through java code

    Hi guys,
    I want to run a java class from another class.
    I tried like this..
    Runtime.getRuntime("java C:\test2");But test2 did not run.
    But I could execute this command sucessfully.
    Runtime.getRuntime("javac C\test2.java");
    I could see class file generated for this.
    Please suggest me how to run a java class .....
    Thanks in advance.

    Hi guys,
    I want to run a java class from another class.
    I tried like this..
    Runtime.getRuntime("java C:\test2");But test2 did not
    run.Because the JVM (the java command) does not take a file as its argument. It takes a class name. You'd want something like java -cp C:\ test2
    But I could execute this command sucessfully.
    Runtime.getRuntime("javac C\test2.java");Because javac, unlike java, does take files (rather than a class name) as its argument.

  • I want run some command using java

    hi all,
    i want run command using java code.
    from cell prompt when i run this command 'mysqldump test > /home/DBNAME.sql'
    it will create DBNAME.sql file.
    but i want to run this command using java code
    i tried the following code but it did not work.
    is any other way is their?
    try {
                   Runtime.getRuntime().exec("setxkbmap nudi");
              } catch(IOException ioe) {
                   ioe.printStackTrace();
    thanks in advance
    daya

    hello,
    thanks for replay
    i am sorry the above code should be like this.(the above code is working fine)
    public class ExportTest{
         public ExportTest(){
              try {
                   Runtime.getRuntime().exec("mysqldump test > '/root/DBNAME.sql'");
              } catch(IOException ioe) {
                   ioe.printStackTrace();
              }catch(Exception e){
                   e.printStackTrace();
         public static void main(String args[]){
              new ExportTest();
    }when run above class, it not creating DBNAME.sql file.
    when run in command prompt it creating DBNAME.sql
    ($ mysqldump test > /root/DBNAME.sql)
    but i want to run this command from java code, that's way tried to do using above
    code, but it won't create DBNAME.sql
    is it wrong what i am doing? or any other way?
    thanks inadvace
    daya

Maybe you are looking for

  • Error when creating Advanced Table in Advanced Table

    Hello, I have created an advanced table, which works fine. When I add an inner advanced table to the outer advanced table I receive the following error stack (see below) and I have no idea what is causing this error. I have only added 1 column to the

  • Need Low Resolution Camera App

    Hello, I have an Iphone and an Ipad. I need to be able to take pictures with both and save them to about 100-200Kb or less. I have found a few low res camera apps that will save only as low as 1MB (Procam and Camera+). Could you help me find a camera

  • HOW TO TURN OFF TEXT MESSAGE SOUND AFTER SENT???

    how do you turn this soud off?

  • Critical variance on your orders 500   Internal Server Error

    Hi, I am getting the error when accessing the reports from MyBudget tab in the portal for Critical Variance on your orders.. java.lang.NullPointerException     at com.sap.tc.webdynpro.modelimpl.dynamicrfc.DynamicRFCModel.createModelObject(DynamicRFCM

  • How to find books in my itouch?

    I download a few books from the Itunes store, and  I have an ibook in my touch as well. But the books just don't appear in my itouch, I can't find my books in the ibook shelf or anywhere in my itouch. what do I do?