my dog learned polymorphism
The moose likes Beginning Java and the fly likes Pesky Runtime method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pesky Runtime method" Watch "Pesky Runtime method" New topic

Pesky Runtime method

Raymond Brown

Joined: Jun 15, 2004
Posts: 5
My question is this -

My code sample -

start = new JButton("Start");
start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae)
// Maybe try running ps -aux first and see if it is already running and ask the user if they are
// sure they want to start it again.
String[] s = {"cd /Users/ray/Desktop/Tenable/thunder/thunder/api/ice9/", outputCommand.getText()};
// run the Unix "ps -ef" command
Process p = Runtime.getRuntime().exec(s[0] + s[1]);
//Process p = Runtime.getRuntime().exec(outputCommand.getText());

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s[0] = stdInput.readLine()) != null)

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s[0] = stdError.readLine()) != null)

// Maybe say start over ?? System.exit(0);

catch (Exception e)
System.out.println("[Exception]. Printing Stack Trace");
// System.exit(-1);


When I run this I get the following error:

[Exception]. Printing Stack Trace cd: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(
at ice9.mainWindow$19.actionPerformed(

It can't even cd into the right directory - let alone run the outputCommand String which is "./ice9" a binary. Any help is appreciated. I am working on the latest version of panther osx and using the latest version of eclipse.

To recap I expected the runtime method to basically cd into the right directory and then run the next command in the string array. However I can't even get it to cd into the right dir.
This is basic and the next step would be to run the "ps -aux | grep ice9" command so that I can pull out the appropiate process id. Please help and thanks in advance.

Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
Your system might have cd as a builtin shell command only. Whatever is your shell call it with a "-c" argument, eg.
I agree. Here's the link:
subject: Pesky Runtime method
It's not a secret anymore!