File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes FTP using exec(String command) of Runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "FTP using exec(String command) of Runtime" Watch "FTP using exec(String command) of Runtime" New topic

FTP using exec(String command) of Runtime

Ashish Sarin
Ranch Hand

Joined: Nov 20, 2000
Posts: 432
I have been trying to make a FTP client using exec(...) method of java.lang.Runtime. I coded the client like this:
Process p = (Runtime.getRuntime()).exec("ftp");
After this I obtain the InputStream and OutputSream associated with the process. Now the problem arise:
(1). When I wrap the InputStream into BufferedReader then a call to readLine() method of BufferedReader results in no output
(2). How one can pass the user name and password to this ftp connection using the output stream of the process
Can someone help me find out a simple solution to creating FTP client using just the exec(...) method rather than using Sockets and ServerSockets.
ashish sarin

Author of
Getting started with Spring Framework
Portlets in Action
Spring Roo 1.1 Cookbook
Portlets blogSpring Roo blog
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

Look at the 13.5 JProgressBar in FTP client application example in the magnificient book Swing 2nd ed. by Robinson and Vorobiev.
But consider also what Sun says about Using sun.* packages
[ April 23, 2004: Message edited by: Jose Botella ]
[ April 23, 2004: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

You need to spawn a new thread to read from the OutputStream because its going to block on calls to read. That way you can still get your interactive input into the new process' InputStream.
I think using URL and URLConnection is easier:

or something like that.
[ April 23, 2004: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
Ashish Sarin
Ranch Hand

Joined: Nov 20, 2000
Posts: 432
I was just wondering how to take care of 'ls' command. Will i be able to obtain the directory listing properly if I use exec(...) command ??
ashish sarin
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, if you're reading the stdout stream. Here's a runtime exec exercise I did. It's not 100% bug free: The server portion has the runtime exec stuff.
Have you looked around for FTP APIs for Java? I've seen some open source or free packages mentioned but never tried any. That would give you the best possible control over FTP. This runtime exec stuff will get complicated in a hurry - reading stdout and errout on threads, synchronizing to examine results, etc.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: FTP using exec(String command) of Runtime
It's not a secret anymore!