wood burning stoves 2.0*
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
Author

FTP using exec(String command) of Runtime

Ashish Sarin
author
Ranch Hand

Joined: Nov 20, 2000
Posts: 432
    
    5
Hi
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 www.somesitename.com");
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.
regards
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
Hello,

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
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

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 ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ashish Sarin
author
Ranch Hand

Joined: Nov 20, 2000
Posts: 432
    
    5
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 ??
regards
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: http://www.surfscranton.com/architecture/CmdServer.htm 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
 
 
subject: FTP using exec(String command) of Runtime
 
Similar Threads
Determine CPU Usage On Remote Machine
Using unix commands in java
Where does FTP process runs
Running a Java Process
Communicating with unix command process from java using stream