Runtime in jdk5.0

java core 2005. 3. 26. 04:12
Author Topic: command execution error
Marina JOSEPH
greenhorn
Member # 67293

posted March 02, 2004 01:59 AM      Profile for Marina JOSEPH   Email Marina JOSEPH   Send New Private Message      Edit/Delete Post  Reply With Quote 
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
Corneil du Plessis
greenhorn
Member # 66649

posted March 02, 2004 03:59 AM      Profile for Corneil du Plessis   Email Corneil du Plessis   Send New Private Message      Edit/Delete Post  Reply With Quote 
In Windows you will have to use Runtime.getRuntime().exec("cmd /c dir /s")
The process you want to envoke is the standard command processor. You will notice in JDK 1.5 that System.getEnv is not deprecated anymore. It will allow you to do Runtime.getRuntime().exec(System.getEnv("ComSpec") + " /c dir /s") which will work on all Windows platforms, because you will find that cmd.exe is not available on all platforms.

Posts: 11 | Registered: Feb 2004  |  IP: Logged
Ernest Friedman-Hill
sheriff and author
Member # 52711

posted March 02, 2004 04:00 AM      Profile for Ernest Friedman-Hill   Author's Homepage   Email Ernest Friedman-Hill   Send New Private Message      Edit/Delete Post  Reply With Quote 
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      Profile for Tim Holloway   Author's Homepage   Email Tim Holloway   Send New Private Message      Edit/Delete Post  Reply With Quote 
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      Profile for Ernest Friedman-Hill   Author's Homepage   Email Ernest Friedman-Hill   Send New Private Message      Edit/Delete Post  Reply With Quote 
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]

'java core' 카테고리의 다른 글

jdk 5.0) In runtime, java compile.  (0) 2005.04.25
jar  (0) 2005.04.25
Annotation  (0) 2005.03.18
Java condition variable  (0) 2005.02.18
Threads from aritma  (0) 2005.02.12
Posted by '김용환'
,