This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing dir from Java

 
madhesh raj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
madhesh raj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic