Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using wget from java code to download files

 
Konstantinos Vasileiou
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to implement a program which, among other things, will have to download automatically a number of pdf files I have found using Google. Instead of building something from scratch, I assumed that I could invoke the Linux wget:

String commandString="wget http://www.google.com";
try {

Process process = Runtime.getRuntime().exec(commandString);


} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
}


The problem is that I get nothing in the program's folder. Null. Void! i tried also to play a bit with wget's parameters, such as masking the user agent (-U mozilla), setting download limit, etc but still I get nothing downloaded. The command works perfectly from the shell, by the way.
Do you have any ideas what I am doing wrong?
 
Joe Ess
Bartender
Posts: 9319
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
The first thing I'd try is to read in the output stream from the process.
 
Konstantinos Vasileiou
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I figured it out and it may be useful for other newbies as well:
First, read the great tutorial on the Use of Runtime.exec() before playing with it... My final code looks somewhat like the one in the tutorial (I print only the error stream, as the output stream of wget does not *seem* to give anything useful as info - so there is no need for a second thread.)

The problem was, as the 2nd page of the article indicates: "...failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic