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.

Similar Messages

  • 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

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

  • Spanish characters getting garbled while executing command using Java code

    Hi,
    I try to execute a command using java code. output of the command contains spanish characters. Few of these characters getting garbled after the command execution.
    Runtime r = Runtime.getRuntime();
              Process p = null;
    String pgm="ipconfig /all";
              try
                        p = r.exec(pgm);
    BufferedReader br=new BufferedReader(new InputStreamReader
                                  (p.getInputStream()));
    while((val = br.readLine()) != null){ System.out.println(val);
              catch (Exception e)
                        return (null);
    I tried to run the code using -Duser.language=es -Duser.region=ES -Dfile.encoding=Cp850, but this did nt help. I could see the outputs properly in command prompt,
    If i redirect the output to a text file , it is getting garbled,
    Please let me know to solve this issue.

    884543 wrote:
    Hi,
    I try to execute a command using java code. output of the command contains spanish characters. Few of these characters getting garbled after the command execution.
    Runtime r = Runtime.getRuntime();
              Process p = null;
    String pgm="ipconfig /all";
              try
                        p = r.exec(pgm);
    BufferedReader br=new BufferedReader(new InputStreamReader
                                  (p.getInputStream()));
    while((val = br.readLine()) != null){ System.out.println(val);
              catch (Exception e)
                        return (null);
    I tried to run the code using -Duser.language=es -Duser.region=ES -Dfile.encoding=Cp850, but this did nt help. I could see the outputs properly in command prompt,
    If i redirect the output to a text file , it is getting garbled,
    Please let me know to solve this issue.Set the character set to UTF-8 to your InputStreamReader, for More details on usage refer the java api :
    http://download.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html

  • Running unix command using java shows error

    Hi All,
    I am trying to run UNIX move command using Java exec method but its throwing error, unable to rename. Here below is the code i am trying to run to move all files from one directory to another directory. source and destination directory exists.
    public class Demo
        public static void main(String args[])
            try
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec("mv /home/demo1/* /home/demo2");
                int exitVal = proc.waitFor();
                System.out.println("Process exitValue: " + exitVal);
            } catch (Throwable t)
                t.printStackTrace();
    }if i give "mv /home/demo1 /home/demo2" in exec method, its working, with * character its giving problem.
    Please help me in resolving the problem.
    Thank you

    Characters like *, >, &, |, etc. are interpreted by the command shell--that is by the bash, zsh, etc. programs. When you execute a command with ProcessBuilder or Runtime.exec, you're not going through one of those shells, so their interpretation of those characters is not available.
    In your code, the character * is being delivered directly to the mv command (which doesn't think * is anything special), as opposed to being turned into a list of files and directories as it would be when it's interpreted by the shell. The mv command doesn't know anything about the * character being special.
    If you want to have those shell interpretations, you need to execute the command through a shell. One example is like so, but a) you'll want to read up on the methods in exec() that take arrays of String, and b) you'll want to read up on ProcessBuilder, and c) you'll need to check your shell's man pages to see the exact syntax and arguments.
    runtime.exec("/bin/bash -c 'mv x/* y'");

  • Run shell commands using java program

    Hi guys,
    I am trying to run shell commands like cd /something and ./command with arguments as follows, but getting an exception that ./command not found.Instead of changing directory using "cd" command I am passing directory as an argument in rt,exec().
    String []cmd={"./command","arg1", "arg2", "arg3"};
    File file= new File("/path");
    try{
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(cmd,null,file);
    proc.waitFor();
    System.out.println(proc.exitValue())
    BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    catch(Exception e)
    {e.printStackTrace();
    So can anyone please tell me what is wrong with this approach? or is there any better way to do this?
    Thanks,
    Hardik

    warnerja wrote:
    What gives you the idea that the process to execute is called "./command"? If this is in Windows, it is "cmd.exe" for example.It does not have to be cmd.exe in Windows. Any executable or .bat file can be executed as long as one either specifies the full path or the executable is in a directory that is in the PATH.
    On *nix the file has to have the executable bit set and one either specifies the full path or the executable must be in a directory that is in the PATH . If the executable is a script then if there is a hash-bang (#!) at the start of the first line then the rest of the line is taken as the interpreter  to use. For example #!/bin/bash or #!/usr/bin/perl .
    One both window and *nix one can exec() an interpreter directly and then pass the commands into the process stdin. The advantage of doing this is that one can change the environment in one line and it  remains in effect for subsequent line. A simple example of this for bash on Linux is
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    public class ExecInputThroughStdin
        public static void main(String args[]) throws Exception
            final Process process = Runtime.getRuntime().exec("bash");
            new Thread(new PipeInputStreamToOutputStreamRunnable(process.getErrorStream(), System.err)).start();
            new Thread(new PipeInputStreamToOutputStreamRunnable(process.getInputStream(), System.out)).start();
            final Writer stdin = new OutputStreamWriter(process.getOutputStream());
            stdin.write("xemacs&\n");
            stdin.write("cd ~/work\n");
            stdin.write("dir\n");
            stdin.write("ls\n");
            stdin.write("gobbldygook\n"); // Forces output to stderr
            stdin.write("echo $PATH\n");
            stdin.write("pwd\n");
            stdin.write("df -k\n");
            stdin.write("ifconfig\n");
            stdin.write("echo $CWD\n");
            stdin.write("dir\n");
            stdin.write("cd ~/work/jlib\n");
            stdin.write("dir\n");
            stdin.write("cat /etc/bash.bashrc\n");
            stdin.close();
            final int exitVal = process.waitFor();
            System.out.println("Exit value: " + exitVal);
    }One can use the same approach with Windows using cmd.exe but then one must be aware of the syntactic differences between commands running in .bat file and command run from the command line. Reading 'help cmd' s essential here.
    The class PipeInputStreamToOutputStreamRunnable in the above example just copies an InputStream to an OutputStream and I use
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    public class PipeInputStreamToOutputStreamRunnable implements Runnable
        public PipeInputStreamToOutputStreamRunnable(InputStream is, OutputStream os)
            is_ = is;
            os_ = os;
        public void run()
            try
                final byte[] buffer = new byte[1024];
                for (int count = 0; (count = is_.read(buffer)) >= 0;)
                    os_.write(buffer, 0, count);
            } catch (IOException e)
                e.printStackTrace();
        private final InputStream is_;
        private final OutputStream os_;
    }

  • Running System Commands using Java

    HI,
    I am developing an application using java which requires some system commands to be run.For example i have to write a java function which can program the windows scheduler to run a particular executable at some time & another one to initiate an ftp.I however do not know how to execute the corresponding commands from java.Is there any way or some specific api(similar to the system command in c) that i can use to perform these operations.I am using j2sdk 1.4.0_01 on a win 98 machine to develop the application.

    See [url http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html]java.lang.Runtime

  • Running Dos Command using Java

    How do i run a particular Dos command using a simple java class?

    Process process = Runtime.getRuntime().exec( ... );
    Search the forum. There are plenty of examples.

  • 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

  • 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

  • RMI :- how to know that rmi registry is running or not using java code

    hi
    I want to know that how to check wather rmi registry is running or not by program in java
    plz any one reply

    Simple method - try to connect to it and see if you get an exception.
    Since the registry can be running on a remote machine, the only simple way
    is to try connecting.
    If the registry is running on a local machine you can also use OS tools (like "ps" on unix) to check whether the process is running, or network tools (e.g. netstat) to check whether it's listening on its TCP/IP port.
    Genady

  • 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 ls command from Java stroed procedure no output

    Hi ,
    I am trying to run ls command from java stored procedure in oracle
    Process p = Runtime.getRuntime().exec("ls");
    BufferedReader stdInput = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new
    InputStreamReader(p.getErrorStream()));
    // read the output from the command
    System.out.println("output of the command run:\n");
    while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
    from java stored procedure in oracle.
    i get output of println statments but it does not go into while loop to print from stdInput.
    Result of running Java stored procedure is -
    output of the command run:
    Call completed.
    when i run the program on client side it works fine.
    Has anybody tried this from java stroed procedure.
    Thanks,
    Jag

    Jag,
    Actually, the question of whether it works for me seems to depend on the version of the OS (or Oracle). On RedHat Linux (Oracle 8.1.6) it didn't work at all, but on Solaris (Oracle 9.0.2) it did. Here's the output from that run:
    SQL> /
    output of the command run:
    init.ora
    initDBPart9i.DBPSun01.ora
    initdw.ora
    lkDBPART9I
    orapw
    orapwDBPart9i
    spfileDBPart9i.ora
    Done
    PL/SQL procedure successfully completed.
    But, I did need to change a line of your code to this:
    Process p = Runtime.getRuntime().exec("/usr/bin/ls");
    your original was:
    Process p = Runtime.getRuntime().exec("ls");
    You might consider, if possible, use of some of the Java File classes instead of ls, as this might make things more predictable for you. There were some examples in oramag.com a few months ago, but they were pretty simple (you might not need them).
    Hope this helps,
    -Dan
    http://www.compuware.com/products/devpartner/db/oracle_debug.htm
    Debug PL/SQL and Java in the Oracle Database

  • Facing problem with running exe from my java code

    Hello,
    I have to run the exe from java code for which I m using Runtime and Process classes. The exe runs in the following way on command line
    C:/ myexe -j [now press enter]
    input sentence followed by ! [now press enter]
    And after this exe gives the output.
    So is there any way to achieve this programmatically in Java?
    Thanks in advance.
    Regards,

    I m confused .. how can i pass input to the Process through InputStream? I think it will need OutputStream for that.
    Here is my code...
    import java.io.*;
    class CallToExe
    static BufferedReader br;
    public static void main(String[] args)
    File F = new File("C:/Chasen.exe");
    try
    if( F.exists())
         System.out.println("Exe exists");
         Runtime rt = Runtime.getRuntime();
         Process proc = rt.exec("C:/chasen.exe");
         br = new BufferedReader(new InputStreamReader(in));
         int a = in.read();
         System.out.println(a);
         String res = br.readLine();
         System.out.println(res);     
         while(!res.equals(null))
         System.out.println(res);     
         res = br.readLine();
    proc.waitFor();
         proc.destroy();
    catch(NullPointerException npe)
         npe.printStackTrace();
    catch (Exception IOEx)
    IOEx.printStackTrace();
    But it doesnt work as my chasen.exe needs arguments from stdin.
    The way exe works is as follows.
    on command prompt>>
    C:/chasen.exe -j [ Need to press ENTER here]
    input statement . [ Need to press ENTER here too]
    Then output is displayed on command prompt which can be taken from InputStream of the process. But I m not able to properly run this exe itself with all the arguments frm Stdin

  • Running bat files in java code

    Hi,
    I am trying to run a .bat file within java code like this.
    Runtime.getRuntime().exec("c:\\Test.bat");
    but no success. Could you please suggest how to run .bat file or simply a
    DOS command from java code.
    thanx in advance
    Deepak Garg.

    try this...
    n reply whether it worked or not......
    import java.util.*;
    import java.io.*;
    import java.net.*;
    try
    Runtime runtime = Runtime.getRuntime();
    Process process ;
    process= runtime.exec("./temp_install");
    //code to print command line replies
    InputStream stderr = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while((line=br.readLine())!=null)
    System.out.println(line);
    catch(Throwable t)
    t.printStackTrace();
    }

Maybe you are looking for