4 Replies Latest reply: Jan 25, 2004 9:56 AM by 796254 RSS

    Runtime.exec

    807582
      Hi,

      i am trying to simulate a shell using Runtime.exec()

      First problem: I cannot browse my filesystem, because the Runtime.exec Command creates a new process every time it is called. How can I solve this problem best? Does my program have to remain the current dir every time the command "cd" was used? Or can i get the dir from the process itself?

      Second problem: My Program gets the standard input and the error input stream. But with some commands (like ftp or telnet) i get no input at all.

      Maybe it is better if I will attach some of the code, for is it difficult to explain
      ...
      /**
            * f�ht einen Befehl in der Konsole aus.
            * @param command ein Kommando
            * @param wait, true, wenn auf den Output des Kommandos
            * gewartet werden soll
            */
           public String exec(String command, boolean wait) {
                
                try {
                     buff = new StringBuffer();
                     
                     // log
                     if (ui != null)
                          ui.addMessage("trying to execute command: " + command);
      
                     // OS ermitteln
                     String osName = System.getProperty("os.name");
                     String[] cmd = new String[3];
                     if (osName.equals("Windows NT") || osName.equals("Windows XP")) {
                          cmd[0] = "cmd.exe";
                          cmd[1] = "/C";
                          cmd[2] = command;
                     } else if (osName.equals("Windows 95")) {
                          cmd[0] = "command.com";
                          cmd[1] = "/C";
                          cmd[2] = command;
                     } else if (osName.equals("Linux")) {
                          cmd[0] = "/bin/sh";
                          cmd[1] = "-c";
                          cmd[2] = command;
                     } else {
                          cmd[0] = "";
                          cmd[1] = "";
                          cmd[2] = command;
                          System.out.println("OS nicht spezifiziert: " + osName);
                     }
      
                     Runtime rt = Runtime.getRuntime();
                     proc = rt.exec(cmd);
      
                     // any error message?
                     StreamGobbler errorGobbler =
                          new StreamGobbler(proc.getErrorStream(), "ERROR");
      
                     // any output?
                     StreamGobbler outputGobbler =
                          new StreamGobbler(proc.getInputStream(), "OUTPUT");
      
                     
                     // kick them off
                     errorGobbler.start();
                     outputGobbler.start();
      
                  // eine Zeitlang auf den Output warten
                     if (wait) {                    
                        outputGobbler.join(2000);                  
                        errorGobbler.join(2000);               
                     }          
           
      
                     System.out.println(buff.toString());
                     // ui.addMessage(buff.toString());
      
                     String s = buff.toString();
                     
                     
                     //this.workDir =
                     
      
                     return s;
      
                } catch (Throwable t) {
                     t.printStackTrace();
                     return null;
                }
           }
      
          /**
           * Absorbiert die Eingabestr�me (Resultate der Befehle
           * in der Konsole)
           * @author tobi
           */
           class StreamGobbler extends Thread {
                InputStream is;
                String type;
      
                StreamGobbler(InputStream is, String type) {
                     this.is = is;
                     this.type = type;
                }
      
                public void run() {
                     try {
                          InputStreamReader isr = new InputStreamReader(is);
                          BufferedReader br = new BufferedReader(isr);
                          String line = null;
                                              
                          while ((line = br.readLine()) != null)
                               buff.append(line + "\n");
                     } catch (IOException ioe) {
                          ioe.printStackTrace();
                     }
                }
                
           }
        • 1. Re: Runtime.exec
          800387
          It is usually much easier to create a java.io.File instance and navigate using its methods. Unless you are writing the equivalent of a screen-scraper to get Unix permissions or something of that sort, the File methods should be more than you need to display file names, sizes, some permissions and to navigate around directories.

          Runtime.exec() making a new process for every call does make sense. How can Java and, say, a shell communicate with anything other than command line parameters and return arguments. You might be able to open a shell and then call getInputStream() and pass the commands through using the stream. But this is a bit of a cluj.

          - Saish

          "My karma ran over your dogma." - Anon
          • 2. Re: Runtime.exec
            807582
            Using File would be fine, of course. But my "Shell" is made for a client-server program and it shall not only be able to do the file operations. I'd like to code it so that it's nearly shell-like for the user. My "RemoteShell" should be able to execute all kind of commands.
            • 3. Re: Runtime.exec
              800387
              Try playing around with getInputStream() and getOutputStream() from the Process object returned by exec(). My hunch is that getInputStream() will behave like System.in and you can pass commands to it. But it's just a theory.

              - Saish

              "My karma ran over your dogma." - Anon
              • 4. Re: Runtime.exec
                796254
                Everyone who wants to use Runtime.exec() should read this:

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

                Just in case you haven't seen it. - MOD