• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Executing Dos based command

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello javaranch,
I am exploring Runtime Class
The following code works fine
Runtime.getRuntime().exec("notepad.exe");
but when i try to run dos command using Runtime class like
Runtime.getRuntime().exec("Dir");
It is throwing IOException
Plese guide me what is problem?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The command that you run for dos is command.exe in Windows 95/98 and cmd.exe in NT. If you store the return value of your call to Runtime.exec() in a process object, then you can controll the DOS session you created. From there you can get the input and output streams and send dir, copy, or whatever other DOS command you would like and view the output. Trying to call Runtime.exec("dir") is like trying to run "dir" as a command from Start->run. It won't work because dir isn't an executable program.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that with this:

but I still get an IOException. Is this what you meant?
I don't know why you get an exception here, as you can run command from Start/Run in Windows, or in a command shell. If you call it in a DOS window, it doesn't appear to do anything, but when you 'exit', you see it's there.
 
Praveen Jadhav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bodie,
I have modified by code to
import java.lang.*;
import java.io.*;
class Trial{
public static void main(String args[]){
try{
int a=0;
Runtime r;
Process p;
r = Runtime.getRuntime();
p = r.exec("cmd.exe");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes("dir\n");
dos.flush();
DataInputStream dis = new DataInputStream(p.getInputStream());
String s = null;
while( (s=dis.readLine()) != null)
{
System.out.println(s);
}
p.destroy();
System.out.println("after destroy");
} catch (Exception e){
e.printStackTrace();
}
}
}
and it is working fine.
thanks a lot.
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I tried Praveen's code and recieved a IOException. The only thing I changed was cmd.exe to command.exe. Any ideas?
Thanks.
Yoo-Jin.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should be command.com in Win9x.
rgds
who
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The code given by Praveen does work fine. But, after displaying the output (result of dir in this case), the command window just hangs there and doesn't respond to anything. We have to manually close the window.
Any technique to run the process in the background. (so that the user doesn't actually see what's hapenning. He just needs to get the exit status).

Originally posted by Praveen Jadhav:
Thanks Bodie,
I have modified by code to
import java.lang.*;
import java.io.*;
class Trial{
public static void main(String args[]){
try{
int a=0;
Runtime r;
Process p;
r = Runtime.getRuntime();
p = r.exec("cmd.exe");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes("dir\n");
dos.flush();
DataInputStream dis = new DataInputStream(p.getInputStream());
String s = null;
while( (s=dis.readLine()) != null)
{
System.out.println(s);
}
p.destroy();
System.out.println("after destroy");
} catch (Exception e){
e.printStackTrace();
}
}
}
and it is working fine.
thanks a lot.


 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by madhesh raj:
Hi,
The code given by Praveen does work fine. But, after displaying the output (result of dir in this case), the command window just hangs there and doesn't respond to anything. We have to manually close the window.


It is because of programming flaw.
If you observe clearly, the statement after while loop will not get executed and the message "after destroy" will not appear.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Siva Prasad:
It is because of programming flaw.
If you observe clearly, the statement after while loop will not get executed and the message "after destroy" will not appear.



dos.writeBytes("exit \n");
Include this line after the line
dos.writeBytes("dir \n");
this should work fine.

Sathish.S
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All - I doubt you are paying attention to this thread.
But, on my Win98 system, JDK 1.3, my output window hangs, even after adding a write of "exit\n" to the output stream.
Anybody got any ideas?
Thanks, Guy
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guy,
The native Win32 implementation of Runtime.exec() uses a pipe with a fixed buffer size (512 bytes). If the output on stdout or stderr of the process you are running exceeds this number of bytes, the process will block until you read from the buffer.
See the following Javaworld article for more details:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

------------------
Brian Duff
Oracle JDeveloper Team
reply
    Bookmark Topic Watch Topic
  • New Topic