Runtime.exec

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齢t 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;

}elseif (osName.equals("Windows 95")){

cmd[0] ="command.com";

cmd[1] ="/C";

cmd[2] = command;

}elseif (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();

returnnull;

}

}

/**

* Absorbiert die Eingabestr齧e (Resultate der Befehle

* in der Konsole)

* @author tobi

*/

class StreamGobblerextends Thread{

InputStream is;

String type;

StreamGobbler(InputStream is, String type){

this.is = is;

this.type = type;

}

publicvoid 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();

}

}

}

[5524 byte] By [derMannUndDasMeera] at [2007-9-29 22:05:32]
# 1

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

Saisha at 2007-7-16 2:25:55 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 2
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.
derMannUndDasMeera at 2007-7-16 2:25:55 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 3
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
Saisha at 2007-7-16 2:25:55 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 4
Everyone who wants to use Runtime.exec() should read this: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.htmlJust in case you haven't seen it. - MOD
duffymoa at 2007-7-16 2:25:55 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...