Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FTP using exec(String command) of Runtime

 
Ashish Sarin
author
Ranch Hand
Posts: 432
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ashish Sarin
author
Ranch Hand
Posts: 432
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic