Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

command execution error

 
Marina JOSEPH
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18154
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic