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



      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
                  // eine Zeitlang auf den Output warten
                     if (wait) {                    
                     // ui.addMessage(buff.toString());
                     String s = buff.toString();
                     //this.workDir =
                     return s;
                } catch (Throwable t) {
                     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) {
        • 1. Re: Runtime.exec
          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
            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
              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
                Everyone who wants to use Runtime.exec() should read this:


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