aspose file tools*
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pesky Runtime method" Watch "Pesky Runtime method" New topic
Author

Pesky Runtime method

Raymond Brown
Greenhorn

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()};
try
{
// run the Unix "ps -ef" command
Process p = Runtime.getRuntime().exec(s[0] + s[1]);
p.waitFor();
//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)
{
System.out.println(s);
}

// 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)
{
System.out.println(s);
}

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

}
catch (Exception e)
{
System.out.println("[Exception]. Printing Stack Trace");
e.printStackTrace();
// 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.

Ray
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: http://aspose.com/file-tools
 
subject: Pesky Runtime method