• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Pesky Runtime method  RSS feed

Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
java.io.IOException: cd: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:54)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:566)
at java.lang.Runtime.exec(Runtime.java:428)
at java.lang.Runtime.exec(Runtime.java:364)
at java.lang.Runtime.exec(Runtime.java:326)
at ice9.mainWindow$19.actionPerformed(mainWindow.java:375)

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.

Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your system might have cd as a builtin shell command only. Whatever is your shell call it with a "-c" argument, eg.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!