Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Runtime.exec() problem

fzammettifzammetti Member Posts: 68
Hello all... I'm working on a facility for a file manager application I have that will allow a person to enter an arbitrary command and execute it. It should act just like sitting at a command prompt, whatever command is entered should be executed. This is Windows-only (for now anyway). Here's my problem... A quick little test class:

public class test {
public static void main(String args[]) {
try {
String cmd = ""rename c:\temp\main.h c:\temp\test.h"";
boolean result = true;
String[] ca = new String[3];
ca[0] = "cmd.exe ";
ca[1] = "/C";
ca[2] = cmd;
Process p = Runtime.getRuntime().exec(ca);
//int e = p.waitFor();
//if (e != 0) { System.out.println("error"); }
} catch (Exception e) {
System.out.println(e);
}
}
}

As near as I can tell, this should work, but it's not. The file does not get renamed. No exceptions are thrown, just nothing happens. If I uncomment out the two lines in there (which is what should be there in the real thing), the app hangs, nothing is displayed, just hangs.

Anyone have any ideas what I might be doing wrong? Thanks all!



Comments

  • fzammettifzammetti Member Posts: 68
    Problem solved... The following works completely as expected...

    import java.io.*;

    public class test {
    public static void main(String args[]) {
    try {
    String cmd = "rename c:\temp\main.h test.h";
    String[] ca = new String[3];
    ca[0] = "cmd.exe";
    ca[1] = "/C";
    ca[2] = cmd;
    Process p = Runtime.getRuntime().exec(ca);
    StreamGobbler eg = new StreamGobbler(p.getErrorStream(), "ERROR");
    StreamGobbler og = new StreamGobbler(p.getInputStream(), "OUTPUT");
    eg.start();
    og.start();
    int ev = p.waitFor();
    System.out.println(ev);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    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) {
    System.out.println(type + ">" + line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    : Hello all... I'm working on a facility for a file manager application I have that will allow a person to enter an arbitrary command and execute it. It should act just like sitting at a command prompt, whatever command is entered should be executed. This is Windows-only (for now anyway). Here's my problem... A quick little test class:
    :
    : public class test {
    : public static void main(String args[]) {
    : try {
    : String cmd = ""rename c:\temp\main.h c:\temp\test.h"";
    : boolean result = true;
    : String[] ca = new String[3];
    : ca[0] = "cmd.exe ";
    : ca[1] = "/C";
    : ca[2] = cmd;
    : Process p = Runtime.getRuntime().exec(ca);
    : //int e = p.waitFor();
    : //if (e != 0) { System.out.println("error"); }
    : } catch (Exception e) {
    : System.out.println(e);
    : }
    : }
    : }
    :
    : As near as I can tell, this should work, but it's not. The file does not get renamed. No exceptions are thrown, just nothing happens. If I uncomment out the two lines in there (which is what should be there in the real thing), the app hangs, nothing is displayed, just hangs.
    :
    : Anyone have any ideas what I might be doing wrong? Thanks all!
    :
    :
    :
    :



Sign In or Register to comment.