How to run a unix command in java

Hi all,
I need to run a unix command in my standalone java application.my java program is in windows.Is it possible to run unix command on windows system.help me out.
I tried this
Process p = Runtime.getRuntime().exec("ps -ef");
            BufferedReader stdInput = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));i get IO Exception -Create Process ps-ef error=2

If you want to run a command from java for a windows platform, you should check windows knows the command.
So, load up a DOS prompt and type ps -ef and see what happens.

Similar Messages

  • How to execute a Unix Command in java

    Hi, Iam trying to execute a unix command on Sun Solaris by passing that command to a java program. How can I achieve this?
    Thanks in advance.

    Have a look at the javadoc around the Runtime.exec() method. If the command is a shell command then you might have to execute a shell as well as the command.
    For example, if you wanted to run a unix command 'ls -l > output.txt' the you might have to pass the following string into the exec() method,
    "/bin/sh ls -l > output.txt'

  • How to run piped Unix commands

    Hi,
    I'm trying to run piped Unix commands using Runtime.getRuntime().exec(...) but apparently the pipe symbol ("|") doesn't seem to work...
    Here is basically what I'm trying to do: running "command1 | command2" where both "command1" and "command2" have dynamic values. I could create a shell script and execute it through Java but there has to be a cleaner way...
    Can anyone help me with this?
    Thanks,
    Thierry

    The | symbol for a pipe is understood by a shell, so it was right to point to this direction.
    If you still have a problem, that might be because of another reason. What did happen, how exactly� did it not work? Did you take care of the stdin and stdout channels of the launched process?

  • Running multiple unix commands form java

    hi i have a probelm when i am trying to run more than one command from java:
    i want to run it by using bin/ksh -c (this will take the first arg so i have probelm with runnig commad like" less abc.txt)
    commands to be run:
    1- ll -tr
    2-mkdir x
    3-less abc.txt
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("/bin/ksh -c ll;pwd;less abc.txt");
    which commands i am allowing to run form the java and how can i make it
    best regrdas
    Thanks in advance.

    eddie100 wrote:
    hi i have a probelm when i am trying to run more than one command from java:
    i want to run it by using bin/ksh -c (this will take the first arg so i have probelm with runnig commad like" less abc.txt)
    commands to be run:
    1- ll -tr
    2-mkdir x
    3-less abc.txt
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("/bin/ksh -c ll;pwd;less abc.txt");
    which commands i am allowing to run form the java and how can i make it
    best regrdas
    Thanks in advance.put your commands in a shell script and then call the script from java ...
    import java.io.*;
    class Runner {
            public static void main(String[] argv) throws Throwable {
                    Process p = Runtime.getRuntime().exec("/export/home/caleb/c_code/t.sh");
                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line;
                    while( (line=br.readLine()) != null ) System.out.println(line);
    }

  • How do I run a unix command to quit ARD if it is running on a remote server I am trying to access?

    how do I run a unix command to quit ARD if it is running on a remote server I am trying to access?

    killall "Remote Desktop"
    Regards.

  • How to execute unix command in java  or jsp

    have a peace day,
    please send some sample code for
    "execute the unix command in java or jsp"
    thank you
    regards
    rex

    i execute this coding
    its compiling. while running i get the error " java.io.IOException: CreateProcess: \ls-l error=2 "
    import java.io.*;
    import java.util.*;
    public class Test
       public static void main(String[] args) throws Exception
         try
              String[] cmd = {"/ls-l"};
    Runtime.getRuntime().exec(cmd);
         catch (Exception e)
               System.out.println(e);
      }what can i do for that
    thank u

  • How can I run Runtime.exec command in Java To invoke several other javas?

    Dear Friends:
    I met a problem when I want to use a Java program to run other java processes by Runtime.exec command,
    How can I run Runtime.exec command in Java To invoke several other java processes??
    see code below,
    I want to use HelloHappyCall to call both HappyHoliday.java and HellowWorld.java,
    [1]. main program,
    package abc;
    import java.util.*;
    import java.io.*;
    class HelloHappyCall
         public static void main(String[] args){
              try
                   Runtime.getRuntime().exec("java  -version");
                   Runtime.getRuntime().exec("cmd /c java  HelloWorld "); // here start will create a new process..
                   System.out.println("Never mind abt the bach file execution...");
              catch(Exception ex)
                   System.out.println("Exception occured: " + ex);
    } [2]. sub 1 program
    package abc;
    import java.util.*;
    import java.io.*;
    class HelloWorld
         public static void main(String[] args){
         System.out.println("Hellow World");
    } [3]. Sub 2 program:
    package abc;
    import java.util.*;
    import java.io.*;
    class HappyHoliday
         public static void main(String[] args){
         System.out.println("Happy Holiday!!");
    } When I run, I got following:
    Never mind abt the bach file execution...
    I cannot see both Java version and Hellow World print, what is wrong??
    I use eclipse3.2
    Thanks a lot..

    sunnymanman wrote:
    Thanks,
    But How can I see both programs printout
    "Happy Holiday"
    and
    "Hello World"
    ??First of all, you're not even calling the Happy Holiday one. If you want it to do something, you have to invoke it.
    And by the way, in your comments, you mention that in one case, a new process is created. Actually, new processes are created each time you call Runtime.exec.
    Anyway, if you want the output from the processes, you read it using getInputStream from the Process class. In fact, you really should read that stream anyway (read that URL I sent you to find out why).
    If you want to print that output to the screen, then do so as you'd print anything to the screen.
    in notepad HelloWorld.java, I can see it is opened,
    but in Java, not.I have no idea what you're saying here. It's not relevant whether a source code file is opened in Notepad, when running a program.

  • 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

  • How to run a openssl command from a java program

    Hi All
    Please suggest on how to run a openssl command from a java program.
    I am using this
    Runtime runtime = Runtime.getRuntime();
    runtime.exec("openssl pkcs8 -inform der -nocrypt test.der result.pem");
    This is suppose to take test.der as input and create result.pem.
    There are no errors but the file result.pem isnt created.
    Thanks in Advance

    First off is that openssl command correct? Should it be this instead:
    openssl pkcs8 -inform der -nocrypt -in test.der -out result.pem
    Try out your openssl command within a command prompt so that you know that it works ok. I think the command line you specified waits on stdin (well it does for me).
    After that.....
    runtime.exec creates a Process object. If you do this:
    Process openssl = runtime.exec("....")
    then you can examine the return code from openssl to see the exit code - for instance if the input file does not exist then exit = 1. You can test for this with Java
    Alternatively you could get the stderr from the process and look inside it - if it is 0 length then all is good, if it has some text in there then it has likely failed. You could then throw an exception and include the stderr output in the exception messgae. You may need to experiment with this, runnig it first when openssl is happy then running it again when openssl is upset.
    M

  • Call an interactive UNIX command from java

    Hi,
    I want to call a UNIX command from java. When issue the command 'htpasswd -c filename username' on UNIX terminal, it asks for the new password and the then confirmation password again (yeah, unfortunately the htpasswd installed on our system does not allow you proivde the password on the command line. So have to work interactively ). Now, I have written a simple java program RunCommand.java. I am hardcoding the password for the htpasswd command in the file (in the real situation, password will be generated dynamically). I want to issue 'java RunCommand' on the UNIX command line and get back the command prompt without being asked for the password twice. The code is below, but the Outputstream does not work as expected. It always asks for inputs. Any idea? Many thanks.
    import java.io.*;
    public class RunCommand {
    public static void main(String args[]) throws Exception {
    String s = null;
    try {
    String cmd = "htpasswd -c filename username ";
    // run a Unix command
    Process p = Runtime.getRuntime().exec(cmd);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    OutputStream stdOut = p.getOutputStream();
    String pswd = "mypassword";
    while ((s = stdInput.readLine()) != null) {
         s = stdInput.readLine();
         stdOut.write(pswd.getBytes());          
         stdOut.flush();
    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null) {
    System.out.println(s);
    stdOut.close();
    stdInput.close();
    stdError.close();
    System.exit(0);
    catch (IOException e) {
    System.out.println("exceptions caught: ");
    e.printStackTrace();
    System.exit(-1);

    There are only about 9 billion responses a day on how to do this. Use the search feature.

  • Execute unix commands from Java

    Hi,
    I have a client application running on windows. This client should connect to a unix server and check for the existence of a file and display the result as "File found/File not found". In order to connect from windows to the unix server, I used the sockets and the connection is successfully established. The second part is to check for the presence of the file in unix server. I searched in google.com and the option I found to execute a unix command from java is the "Runtime.exec()". Runtime.exec is considered as the less effective (not a favorable) one.
    Is there any other option available (other than the Runtime) to execute the unix command from java? Can you please let me know.
    Thanks a lot
    Aishu

    So, please let me know how I can execute the above unix commands without Runtime.exec()You have a client and a server.
    You want something to run on the server, not the client.
    That means that something must in fact being running on the server before the client does anything at all.
    For example telnet. Or a J2EE server application.
    So is something like that running?
    If not then there absolutely no way to do what you want, even with Runtime.exec().
    If yes then what you do depends on what is running. So Runtime.exec() would be pointless if a J2EE server was running.

  • Invoking Unix command with java stored procedure

    Hi,
    I have a perfectly working environment and now I am trying to replicate the same in another server. I have a java stored procedure which invokes the Unix command using java Runtime.exec() in my code named "run". I am sure that the code is called and the java class run is resolved in oracle as I could see that in dba_java_resolvers dictionary. But I am not sure whether the function in java is called or whether a exception is thrown. how to identify this? Could there be any thing to do with settings in oracle side? please help me.
    Thanks in advance,
    Marutha

    Hi,
    Do you get any output while running the code?
    I'm testing similar solution and if there are any errors or Oracle can't execute the program due to permissions or other issues the error information will be displayed. Also in the Java class itself you need to catch exceptions and print stack trace to standard output.
    declare
    x number;
    begin
    dbms_output.enable(1000000);
    dbms_java.set_output(1000000);
    x:=system_command.run_command('testconnect.sh');
    dbms_output.put_line('Returned value='||x);
    exception
    when others then dbms_output.put_line('Sql error='||substr(sqlerrm,1,250));
    end;
    <system_command.run_command> - replace with a call to your java stored procedure.

  • How can i use Unix database in java?

    How can i use Unix database in Java?
    Message was edited by:
    JPro

    I have not a clue about FoxPro, but the db then is FoxPro and not Unix. The better question would be "How do I connect to FoxPro DB running on Unix with JDBC?".
    My answer to that would be, search the Internet for a JDBC driver.

  • How to use cvs checkout command in java

    Hi,
    I am using Ubuntu
    I have installed cvs client in ubuntu
    I have one problem running 'cvs checkout' command through java..
    "cvs" -d ":ext:[email protected]:/var/cvs" co -P "Auto/test"
    When i run the above command in terminal, i got one prompt like '[email protected]'s password:' in the terminal if i give the valid password the folder 'Auto' checked out successfully..
    I have tried following steps but i don't know how to give the password to the terminal using java
    First i created the 'checkout.sh' file.
    In this i have typed "cvs" -d ":ext:[email protected]:/var/cvs" co -P "Auto/test"
    Then i call it using java by the following code
    =================================================================
    String cmd[] = {"bash","checkout.sh"};
    Runtime.getRuntime().exec(cmd).waitFor();
    =================================================================
    Anyone let me know how do I achieve this in java?
    Thanks in Advance
    Kulandaivelu

    masijade. wrote:
    PhHein wrote:
    Maybe you need to modify your links to be more obvious :P
    [*>>klick me<<*|http://www.jcvs.org/]
    Nah! Attention to detail is one of most crucial attributes for a programmer in any language. ;-)
    Edit: The urls seemingly don't like h1 and color tags. ;-)Nope, they don't and I couldn't be bothered to try.
    Gratz on Gold!

  • Having trouble in running a unix command and getting the output

    Hi,
    I am trying to run a unix command from within the java code. I am not able to make it work. I am enclosing the code and the error message that I am getting. Any help is highly appreciated.
    import java.io.*;
    public class RunCommand {
        public static void main(String args[]) {
            String s = null;
            try {
                Process p = Runtime.getRuntime().exec("cat UNIX_ASCII_TEXT_FILE | A_UNIX_PROGRAM -d");
                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("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                // read any errors from the attempted command
                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                System.exit(0);
            catch (IOException e) {
                System.out.println("exception happened - here's what I know: ");
                e.printStackTrace();
                System.exit(-1);
    }The error message that I am getting is
    Here is the standard output of the command:
    SLu|%%$$=
    Here is the standard error of the command (if any):
    cat: cannot open |
    cat: cannot open A_UNIX_PROGRAM
    cat: cannot open -dLooks like the cat command is working and not the pipe command and the command after the pipe. But when I run the UNIX command from the command prompt I get the expected result.

    You might read this article and see if its approach works.
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Maybe you are looking for

  • All rows in pivot table

    Hello Gurus, The problem i am facing is whenever there is no content in the columns the rows are not being shown. I want all the rows to be displayed inspite whether there is data or not. I tried unchecking limit rows based on content as well as i ch

  • Code works in html not in jsp y

    the bello code works in html but not in jsp y <img src="images/logo.gif">this code is well running in html when i saved it as .jsp it doesn't works ie the image is not displaying y. could u help me

  • Images dissapearing when scrolling TileList.

    Hello, In the AIR application I am working on, I need to load a large number of images to a TileList on the clients computer. Since they can't be embedded, they were taking a long time to load since each image could be 3-4mb+. In an attempt to reduce

  • Multiple users Partner Portal

    Hey everybody, I was wondering if it is possible to add users to the Partner Portal level, so they also see all the sites I manage in the BC PP. I can't find this option anywhere.                    Of course I can add a user to the site, but I want

  • Itunes is being stupid - help!

    recently (as is all year) itunes has been telling me it can't locate my music. 60% is in my music folder in my files, and when i locate one from there and then check "use this folder to locate other files" it says it couldn't locate any of my 1500+ s