Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.exec() IOException

 
J Hartley
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have got a piece of code that runs the NT "dir" command and prints the results.
It works fine when run in a standalone class. When I run it in a Servlet, I get
java.io.IOException: CreateProcess: dir error=2
at java.lang.Win32Process.create(Native Method)
Here is the code...
Runtime r=Runtime.getRuntime();
//Process p=null;
//p=r.exec("dir");
Process p = r.exec("dir");
int i;
InputStream os=p.getInputStream();
while((i=os.read()) != -1)
System.out.println((char)i);
System.out.println("<br><b> Script Process ID :"+p+"</b><br>");

I am running in the Tomcat container. Anyone know how I can resolve this?
Thanks,
Joe
 
Erick Jones
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to open up a dos shell in order to run a dos command. For example,
Process p = r.exec("cmd /c dir");
OR
Process p = r.exec("command.com /c dir");
Good luck!
Erick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic