Runtime exec methods

looking at the Runtime API there are two methods listed namely:-
public Process exec(String command)
             throws IOException
    Executes the specified string command in a separate process.and
public Process exec(String[] cmdarray)
             throws IOException
    Executes the specified command and arguments in a separate process.
Question:-
Can the second method listed be used to pass in an array of different commands or not, to be executed one after the other?

I am on window so when I do rt.exec(new String [] {"dir", "c:\\oracle});{code}
I get the following error.
Exception: CreateProcess: dir c:\oracle error=2                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • The Runtime.exec methods doesn't work well on Solaris ???

    I have two threads and I set the different running time.
    I use Runtime.exec to a run the command and use Process to get the process.
    It works properly in the windows2000 platform.
    However, when I transfer the platform to Solaris...and run the program...
    Two threads always at the same time....It is very wired....I always debug
    for 2 days....
    (at first I run "vmstat 1 2" command, later I change to "ls","rmdir"....etc,
    all of them don't work.....
    If I close the Runtime.exec..........Everything works well......)
    And I study the API. I found this message...
    The Runtime.exec methods may not work well for special processes on certain
    native platforms, such as native windowing processes, daemon processes,
    Win16/DOS processes on Win32, or shell scripts. The created subprocess does
    not have its own terminal or console.
    Could someone share her/his experience.....:(
    And if any other way I can run command inside java code instead of
    Runtime.exec.....???
    Please reply my mail to [email protected] I do appreciate your kindly &
    great help!!!!!!!!
    This is my code.......
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    * <p>ServerThread1</p>
    * <p>??�X???��?�D???�X???, "Vmstat 1 2".</p>
    class ServerThread1 extends Thread{
    private ServerAgent Sa;
    public ServerThread1 (String Name, ServerAgent Sa){
    super(Name);
    this.Sa = Sa; file://Assign ServerAgent reference Sa
    public void run(){
    while(true){
    try{
    Thread.sleep(5000);
    catch (Exception e){
    System.out.println("ServerThread1 fails");
    System.out.println("Thread1 is running.");
    try {
    Runtime rt1 = Runtime.getRuntime();
    Process proc1 = rt1.exec("mkdir"); ------>If I close
    rt1.exec , two threads works seperately...........:(
    catch (Exception e) {
    System.out.println("Thread1 Error");
    class ServerThread2 extends Thread{
    private ServerAgent Sa;
    public ServerThread2 (String Name, ServerAgent Sa){
    super(Name);
    this.Sa = Sa;
    public void run(){
    while(true){
    try{
    Thread.sleep(15000);
    catch (Exception e){
    System.out.println("ServerThread2 fails");
    System.out.println("Thread2 is running.");
    try {
    Runtime rt2 = Runtime.getRuntime();
    Process proc2 = rt2.exec("vmstat 1 2"); ----->If I don't run
    the rt2.exe, two threads work seperately....
    catch (Exception e) {
    System.out.println("Thread2 Error");
    public class ServerAgent{
    private Vector v1 = new Vector();
    private Vector v2 = new Vector();
    private Hashtable currentData = new Hashtable();
    private static String startUpSwap = null;
    private static String startUpMem = null;
    public static void main(String[] arg) {
    ServerAgent s = new ServerAgent();
    ServerThread1 st1 = new ServerThread1("Thread1",s);
    ServerThread2 st2 = new ServerThread2("Thread2",s);
    st1.start();
    st2.start();

    If I close the Runtime.exec..........Everything works
    well......)You don't empty the output of the command, that blocks the process.
    A citation from
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    Why Runtime.exec() hangs
    The JDK's Javadoc documentation provides the answer to this question:
    Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
    Try out something like this:
    String s;
    try {
       Process myProcess =
       Runtime.getRuntime().exec("ls -l"));
       DataInputStream in = new DataInputStream(
              new BufferedInputStream(myProcess.getInputStream()));
        while ((s = in.readLine()) != null) {
            out.println(s);
    catch (IOException e) {
        out.println("Error: " + e);
    }Another source of trouble under Unix is not having the correct permission for that user that executes the Java VM, which will be the permissions for the spawned subprocess. But this probably not the case, as you see something after exit.
    Regards,
    Marc

  • Java's Runtime.exec() method

    When you shell out to java's Runtime.exec() method, are the process name and arguments the same for the child process that is spawned.
    We see duplicated processes about the time when our logs tell us this command was run. However, we cannot seem to reproduce this. Has anyone else seen anyone this before?

    That's what I though too. But check this out . . .
    Our code looks as follows:
    private Runtime rt;
    private Process p;
    rt = Rutime.getRuntime();
    p = rt.exec(command);
    This exec() call creates a new process, which is a child of the java process that runs this command. The final process looks like the "command" string that is passed to the exec() method call. In our case, the command is a call to the /usr/bin/mail utility to send out faxes and emails.
    We ran a very tight loop executing the rt.exec() call over and over. What we found was that for a minor fraction of a second, the newly created process looks just like the original process including the same arguments. However, the PID's indicated that one process was the child of the other. This is why it looked like we had 2 of the same processes. WILD!
    Thanks guys!

  • Spawn a java process using runtime.exec() method

    Hi,
    This is my first post in this forum. I have a small problem. I am trying to spawn a java process using Runtime.getRuntime().exec() method in Solaris. However, there is no result in this check the follwoing program.
    /* Program Starts here */
    import java.io.*;
    public class Test {
    public static void main(String args[]) {
    String cmd[] = {"java", "-version"};
    Runtime runtime = Runtime.getRuntime();
    try{
    Process proc = runtime.exec(cmd);
    }catch(Exception ioException){
    ioException.printStackTrace();
    /* Program ends here */
    There is neither any exception nor any result.
    The result I am expecting is it should print the following:
    java version "1.4.0"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
    Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
    Please help me out in this regard
    Thanks in advance
    Chotu.

    Yes your right. It is proc.getInputStream() or proc.getErrorStream(). That is what I get for trying to use my memory instead of looking it up. Though hopefully the OP would have seen the return type of the other methods and figured it out.

  • Runtim exec() method not working.....giving Exception

    whts the code to use exec() method. i m using it as follows and also catching exception.
    Runtime r=Runtime.getRuntime();
    Process p=r.exec("java A");
    code is giving IOException at runtime with error=2.

    Here is an example I used when I first needed to test the runtime.exec
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    public class TestRuntime {
        public static void main(String args[])
            try
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec("java A");
                InputStream stderr = proc.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                System.out.println("<ERROR>");
                while ( (line = br.readLine()) != null)
                    System.out.println(line);
                System.out.println("</ERROR>");
                int exitVal = proc.waitFor();
                System.out.println("Process exitValue: " + exitVal);
            } catch (Throwable t)
                t.printStackTrace();
    }Are you waiting for the process to end before exiting your program?

  • Problem to execute cvs command using Runtime.exec method

    Hello,
    I want execute this cvs command, with this options:
    cvs -d :pserver:[email protected]:/home/cvs/cvsroot rlog -S -d "2007/05/01<now" Project
    I tried to execute with Runtime.exec() :
    Runtime.exec("cvs -d :pserver:[email protected]:/home/cvs/cvsroot rlog -S -d \"2007/05/01<now\" Project");
    But I have an error because the smaller character is interpretate as a redirection, no as a smaller symbol.
    How I can do to use this command with Runtime.exec ?
    Thanks.
    Regards.

    Sorry,
    I had a typing mistake.
    I want say:
    Runtime.exec("cvs -d :pserver:[email protected]:/home/cvs/cvsroot rlog -S -d \"2007/05/01<now\" Project");
    Regards.

  • Using Runtime exec() method to run java files and return runtime errors

    Hi
    I'm writing a java editor and I use
    Runtime.getRuntime().exec(command)
    to compile the java files. That works fine and I deal with the returned errors using the getErrorStream().
    My questions are:
    1. Can I use the same technique for returning runtime errors. In any posts I've read the process runs from begining to end, returning the errors after completion. How do I return the errors of the app as they happen interactively?
    2. If i cant use the exec and getErrorStream() methods then does anyone know how it is done?
    Thanks in advance for any help!

    Read this:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    MOD

  • How to disable Runtime.exec method ?

    i want to disable some method in java like Runtime.getRuntime().exec() and Runtime.getRuntime().totalMemory()
    can i implement it by change java.policy file ,and how to do it?

    You can use Set Menu Item Info with items from a runtime menu file. You don't need to insert items programmatically to use Set Menu Item Info. Use the Current VI's Menubar function to get the menubar refnum, then call Set Menu Item Info along with three inputs:
    1. The menubar refnum.
    2. The item tag for the item to be disabled or enabled. You may need to go back to the menu editor to get the tag, which may or may not be the same as the displayed item name.
    3. A boolean wired to enabled.
    I referred you to the examples just to see one part of the diagram.

  • Problem with Runtime.exec() method

    Hi All,
    tring _cmd = "cmd /c ";
              cmd =cmd+"sqlldr ";
              cmd = cmd + " userid=" + userId + "/" + passwd + "@"+ tnsEntry;
    cmd = cmd + " control=" + controlFilePath;
              cmd = cmd + " log=sql.log skip=1";
              System.out.println(_cmd);
         try{
                             Runtime r = Runtime.getRuntime();
                             Process process = r.exec(_cmd);
                             int exitVal = process.waitFor();
    System.out.println("Process exitValue:********** " + exitVal);
                   catch(RuntimeException re )
                        System.out.println("Failed to runtime run the process.123.."+re);
    I ve used proc.exitValue() to print the value of int value
    but i got exception
    then i used proc.waitFor() and print the value of int I got value = 4
    here it is
    Process exitValue:= 4
    could u please tell me what does it mean? how to solve if the value is 4?
    Thanks in advance

    Please don't cross-post, it is extremely rude.
    Stick with the [original thread|http://forums.sun.com/thread.jspa?threadID=5329691&tstart=0].

  • 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

  • Problem with runtime.exec().It hangs Up

    Hi all,
    I am having a problem with the runtime.exec method.I am trying to execute linux commands using this method.For most of the commands it works fine.But when i tried to change the user with the su command in linux my program hung up.So please help me to get around this.any help would be highly appreciable..
    I am pasting the code..
    <code>
    Process p=null;
    int ch=0;
    try
    System.out.println("Before executing command");
    String unlock_command="sh changeuser.sh";
    p = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c","su tony"});
    InputStreamReader myIStreamReader = new InputStreamReader(p.getInputStream());
    while ((ch = myIStreamReader.read()) != -1)
    System.out.print((char)ch);
    p.waitFor();
    int p_exitvalue = p.exitValue();
    System.out.println("After executing the command and the exit value = "+p_exitvalue);
    p.destroy();
    catch (IOException anIOException)
    System.out.println(anIOException);
    catch(Exception e)
    e.printStackTrace();
    </code>
    Thanks
    HowRYou

    Hi sabre,
    What you have pointed out is right.But if i change the user as root then it will not ask for a password.Isn't it.Anyway thank you for giving your suggestions.Can yoiu help me more.Waiting for all of your help.I will try to swoitch between different users othere than root by giving the password.So just help me.

  • Runtime.exec() batch file problem

    Hi folks,
    i have a little problem when I want to execute a batch file through java's runtime.exec() method.
    the execution of a very simple batchfile works (for example opening notepad or other stuff), but I have problems with the following batch file (although it's still simple):
    net use s: \\Dd-nt-fs\Dsmp15_files
    set DSCFG=c:\dscfg
    set PSPATH=c:\adproof
    set ora_path=C:\oracle\ora92
    set path=S:\Dsmp_Q4_2004\Bin;%ora_path%\bin;%path%;
    set nd_font=S:\Dsmp_Q4_2004\Fonts
    set nxPS_FONT_DIR=%ND_FONT%
    set PSPATH=c:\adproof
    set PSFORMS=S:\Dsmp_Q4_2004\PsForms\Telenor
    s:
    cd S:\Dsmp_Q4_2004\bin
    S:\Dsmp_Q4_2004\Dsmp_Reports\Telenor\adproof.exe DBUSER=a_user DBPSWD=a_pswd DBSTR=dbstr ADINFO=adinfo.lst TASKID=a_taskid
    my guess is that there is a problem with the setting of environment variables which correspond to the drive s:
    I mannually added the batchfile (line by line) and looked whether it got through to the command which executes the adproof.exe file: for example the below batchfile worked:
    set DSCFG=c:\dscfg
    set PSPATH=c:\adproof
    set ora_path=C:\oracle\ora92
    s:
    cd S:\Dsmp_Q4_2004\bin
    S:\Dsmp_Q4_2004\Dsmp_Reports\Telenor\adproof.exe DBUSER=a_user DBPSWD=a_pswd DBSTR=dbstr ADINFO=adinfo.lst TASKID=a_taskid
    Here is the java code: in which i execute the batch file:
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("net use s: \\\\Dd-nt-fs\\DSMP15_FILES");
    BufferedReader  b = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
    while( (line=b.readLine())!=null) {
          System.out.println(line);
    int exitVal = proc.waitFor();
    System.out.println("Exit Value = "  + exitVal);
    String[] cmd = new String[1];
    cmd[0] = "C:\\Documents and Settings\\tikmi\\My Documents\\mysources\\DSMP Prototype\\print\\hallo3.bat";
    // here i declared the needed settings of the environment variable and pass that array to the exec() method                   
    String[] env = new String[8];
    env[0] = "DSCFG=c:\\dscfg";
    env[1] = "ORA_PATH=C:\\oracle\\ora92";
    env[2] = "PSPATH=C:\\Documents and Settings\\tikmi\\My Documents\\mysources\\DSMP Prototype\\print";
    env[3] = "ND_FONT=S:\\Dsmp_Q4_2004\\Fonts";
    env[4] = "nxPS_FONT_DIR=%ND_FONT%";
    env[5] = "PSFORMS=S:\\Dsmp_Q4_2004\\PsForms\\Telenor";
    env[6] = "ND_PATH=S:\\Dsmp_Q4_2004\\resource";
    env[7] = "PATH=S:\\Dsmp_Q4_2004\\Bin;%ORA_PATH%\\BIN;%ND_PATH%;%PATH%;";
    rt  = Runtime.getRuntime();
    proc = rt.exec(cmd, env);
    b = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
    while( (line=b.readLine())!=null) {
              System.out.println(line);
    exitVal = proc.waitFor();
    System.out.println("Exit Value = "  + exitVal);Maybe someone else had similar problems or can help me solving that problem.
    Any advice would be very appreciated.
    regards
    mirkolino

    finally I got it working now.
    for those who are interested in the problem, my code that's now working is the following
    Runtime rt = Runtime.getRuntime();
                            Process proc = null;
                            String cmd = "cmd.exe /c c:\\adproof\\hallo3.bat";
                            proc = rt.exec(cmd);
                            InputStream in = proc.getInputStream();
                            BufferedReader br = new BufferedReader ( new InputStreamReader(in));
                            String line;
                            while( (line=br.readLine()) != null) {
                                System.out.println(line);
                            int exit = proc.waitFor();
                            System.out.println();
                            System.out.println("Process exited with: " + exit);the problem was, that i have to handle and read the input stream
    thanks for you help
    greetings from mirkolino

  • Runtime.exec error on Fedora Core

    Hi,
    I have some difficulty using Runtime.exec method on Fedora Core.
    If classpath contains "space", it cannot execute a commnad.
    It works well on Windows systems with quotation marks.
    Here's my code.
    Proccess p = r.exec("java -classpath .:\"pkg root"\" A");
    Java command regards root as a class. It is actually a half of directory name. How can I solve this problem?
    thank you for reading.

    This setting works for me on Linux:
    [ebank2@giskard ivan]$ java b
    here is a - have a nice day![ebank2@giskard ivan]$ cat "a b"/a.java
    public class a {
    public static void main (String a[] ) {
    System.out.println("here is a - have a nice day!");
    }[ebank2@giskard ivan]$ cat b.java
    import java.io.*;
    public class b {
    static String s[]={ "java" , "-classpath" , "a b" , "a" };
    public static void main (String a[] ) throws Exception {
    Process p =  Runtime.getRuntime().exec(s);
    InputStream is = p.     getInputStream() ;
    int c;
    while((c=is.read()) != -1) System.out.print((char)c);
    }

  • RSH, Runtime.exec() & Exitcodes

    hi
    I am using RSH.exe on a Windows machine, which is used to access my server machine, running AIX 4.3. All I want to do is create a directory on the server that is required by the my Java program, so that the files that the program will be copying to the server can be placed into the new directory created. I can accomplish all this. To execute the RSH.exe I use Runtime.exec() method. However, when I use the Process.waitFor() method the program hangs... therefore no exit value is returned and it is this exit value that I require! The output from the process is displayed for both normal output and error output... can anyone shed any light onto this situation???? pleeeeeease! it is really wrecking my head now....
    Regards,
    Finbarr.

    hi
    unfortunately the thread idea has yielded no results! the problem still exists...... it is driving me mad!
    Finbarr.

  • Runtime.exec() does not work?

    I'm trying to invoke a C++ executable from java using the Runtime.exec() method. The C++ application accepts a filename as a command line argument & opens the file. This C++ app is unicode enabled i.e. it can accept UTF-16 (wide char) parameters. Howevere, when i invoke this application using Java's Runtime.exec() and specify a japanese file name as an argument, the japanese characters get converted to '?' characters by the time they are received in the C++ application. I'm running this application on Windows 2K, default i.e. English version.
    Looking at the source code of Runtime class, it seems that the exec()
    function makes use of a native helper function - execInternal(). Does
    this function support the entire unicode range?
    Is there any way we can avoid the conversion of japanese characters to '?' characters? Also, is there any other alternative for invoking an external application with Unicode (Say, japanese) arguments?
    Please reply ASAP.
    Thanks!

    >
    I'm trying to invoke a C++ executable from java using
    the Runtime.exec() method. The C++ application accepts
    a filename as a command line argument & opens the
    file. This C++ app is unicode enabled i.e. it can
    accept UTF-16 (wide char) parameters. Howevere, when i
    invoke this application using Java's Runtime.exec()
    and specify a japanese file name as an argument, the
    japanese characters get converted to '?' characters by
    the time they are received in the C++ application. I'm
    running this application on Windows 2K, default i.e.
    English version.
    Looking at the source code of Runtime class, it seems
    that the exec()
    function makes use of a native helper function -
    execInternal(). Does
    this function support the entire unicode range?I don't know because I've never tested this case specifically.
    You didn't show your code though. How are you reading in the String? You mentioned that you passed a Japanese character String as a filename argument. I also read that you are running on an English Win2K platform. How did you read that argument in? It may just be that you read the argument in your default encoding(English) and you needed to specify an alternate one.

Maybe you are looking for

  • Issue check to customer

    Hi Gurus, I have a business scenario: Customer comes to store and picks up the items and pay the item.  I use F.13 to clear customer open items. When customer comes for exchange, customer returns item and pick ups another item i receive return sales

  • LEA IN 4.7E and ECC 6.0

    Hi All, In our project LEA is in 4.7E R/3 and CRM is in seperate platform. We have to decouple that connection b/w 4.7E R/3 and CRM, we have to upgrade that r/3 to ECC 6.0. and then we have to couple the ECC6.0 and CRM. Now there is any difference in

  • Iphone stays on shuffle,wont play music

    the ipod on my iphone stays on shuffle mode even though it is turned off? it wont even let the play any music then continues to shuffle and wont play any songs. ive tried to do a hard reset on phone and did a restore backup of the phone but nothing s

  • Hide the Bluetooth File Transfer Window

    Can anyone give me any clues as to how I could stop the File transfer WIndow appearing when I Bluetooth an image from phone to my Mac? It is the Obex Agent window. That says Incoming File etc It will appear over any open applications right in the cen

  • Load dimensions' elements in Hyperion Planning 3.5

    Hy all, i have a big problem: i have to load a lot (almost two hundred) of elements in account and entity dimension. Of sure you will understand that it is impossible to manually insert every single element. do you have any idea of as is possible to