Author |
Topic: command execution error |
Marina JOSEPH greenhorn Member # 67293
|
posted March 02, 2004 01:59 AM
import java.io.*;
class cmdEx { public static void main(String[] arg) { String str; Process p; BufferedReader in; try { p = Runtime.getRuntime().exec("/bin/ls -aFl"); in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((str = in.readLine()) != null) { System.out.println(str); } } catch (IOException e) { System.out.println(e.toString()); } }
} message c:\>java cmdEx Error java.io.IOException :CreateProcess :/bin/ls-aFl error=3
this can run p=Runtime.getRuntime().exec("java"); its working
but p=Runtime.getRuntime().exec("dir /p"); from windows. again display the error java.io.IOException :CreateProcess ir /p error=3
Posts: 18 | Registered: Feb 2004 | IP: Logged | |
|
Ernest Friedman-Hill sheriff and author Member # 52711
|
posted March 02, 2004 04:00 AM
The UNIX code should work fine. Be sure there's really a /bin/ls on your system, and that it really accepts the flags you're giving it.
For the Windows "dir" version, note that "dir" is not a separate program, but a command implemented by cmd.exe (or command.com). I believe you need to execute something like
cmd /e "dir /p"
rather than just "dir /p".
Note that this exeact issue is covered in the Java programmer's FAQ.
--------------------
[Jess in Action] [Weblog]
Posts: 7082 | Registered: Jul 2003 | IP: Logged | |
Tim Holloway bartender Member # 15625
|
posted March 02, 2004 09:16 AM
However this is a horrible way to get a list of files for processing. Use the java.io.File.list() method instead. You can add a filter if you like to do the equivalent of wildcards.
Hint: You can normalize filenames to the Unix style: C:\java\myfile.txt can also be accessed in java code (or most config files) as C:/java/myfile.txt.
And that way you don't get burned by forgetting that a backslash is an escape character!
--------------------
If you extend the currently popular business philosophy to its ultimate end, we'll end up with apps produced in no time, for no cost that do nothing. And from the way some major corporate sites work, it won't be much longer.
Posts: 2800 | Registered: Jun 2001 | IP: Logged | |
Ernest Friedman-Hill sheriff and author Member # 52711
|
posted March 02, 2004 09:46 AM
quote:
Originally posted by Tim Holloway: However this is a horrible way to get a list of files for processing.
I agree, but based on her other posts, she's implementing something like a homegrown rexec service; I think executing the commands is the whole point.
[ March 02, 2004: Message edited by: Ernest Friedman-Hill ]
--------------------
[Jess in Action] [Weblog] | |