File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Executing dir from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Executing dir from Java" Watch "Executing dir from Java" New topic
Author

Executing dir from Java

madhesh raj
Ranch Hand

Joined: Nov 28, 2000
Posts: 62
Hi,
I was trying to use the DOS command dir from a Java prog. using Runtime.exec. I got the desired output, but the windows just hangs like that. Even though i destroy the process, the statement After Destroy (see the code below) never gets printed. Can anyone explain me the reason.
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();
BufferedReader dis = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = dis.readLine();
while(s!= null)
{
System.out.println(s);
s = dis.readLine();
}
p.destroy();
System.out.println("after destroy");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Thanks
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The problem is that when the dir command finishes, the cmd.exe process is still running - it's waiting for another command. So the while loop never receives a null indicating that output is finished - it just hangs on the readLine(), waiting for the cmd.exe process to output something else. You can fix this by telling cmd.exe to exit once it's finished:
<code><pre>
dos.writeBytes("dir\n");
dos.writeBytes("exit\n");
</pre></code>


"I'm not back." - Bill Harding, Twister
madhesh raj
Ranch Hand

Joined: Nov 28, 2000
Posts: 62
Hi Jim,

Thanks for your help. It works just fine. I wanted to know one more thing. Suppose i have a command line tool such as PGP which does text encryption. I want to recieve user input from my GUI and encrypt the same by calling the command line tool thru' Runtime.exec. What happens is that it opens up a command window and after execution of the command, it prompts the user to press any key to close the command prompt window.
Is there any way, i can do it in the backend. That is i don't want the user to see the command window. (he shouldn't get any prompt such as "Press any key to continue"). The user should be totally unaware of the process in the background.
Thanks
Madhesh
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm not familiar with the PGP tool specifically, but if it's really a "command-line tool" I'd say it ought to be possible to use it without requiring GUI interaction from the user. Consult the documentation for the tool carefully - there ought to be an option to disable this user prompting. If not, there is probably a better tool available somewhere.
Alternately, if it just wants the user to press a key, you may well be able to do that through the same OutputStream you used to start the pgp process. Try writing another character after the PGP command and newline. If that doesn't work, insert a delay that's long enough to make sure the process is completed, and the write a character. If this works, you may be able to make it more elegant by monitoring the output from the PGP process so that you know when it asks "Press any key", rather than waiting an arbitrary period of time. Still not very elegant - better to find a tool which does not require user GUI input. Good luck.
[This message has been edited by Jim Yingst (edited February 27, 2001).]
 
Don't get me started about those stupid light bulbs.
 
subject: Executing dir from Java
 
Similar Threads
Executing Dos based command
Destroying a child process
Exiting a Runtime.exec()
How to read from DOS pipe?
Hiding Dos Prompt window