| Author |
command execution error
|
Marina JOSEPH
Greenhorn
Joined: Feb 20, 2004
Posts: 16
|
|
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
|
 |
Corneil du Plessis
Greenhorn
Joined: Feb 12, 2004
Posts: 13
|
|
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.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
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][AskingGoodQuestions]
|
 |
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 14491
|
|
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!
|
Customer surveys are for companies who didn't pay proper attention to begin with.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
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 ]
|
 |
himanshu patel
Ranch Hand
Joined: Feb 03, 2003
Posts: 205
|
|
Originally posted by Marina JOSEPH: 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
I have tested this code on Linux and it is working perfectly. However I have one doubt. c:\>java cmdEx Error java.io.IOException :CreateProcess :/bin/ls-aFl error=3 Are you execurting this program on windows? How will you access to Unix/Linux commands on windows? You need to execute on Unix/Linux in order to use /bin/ls-aFl command. [ March 03, 2004: Message edited by: himanshu patel ]
|
If you want to become a rich, do not work for others but make others to work for you.
|
 |
 |
|
|
subject: command execution error
|
|
|