This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Using wget from java code to download files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using wget from java code to download files" Watch "Using wget from java code to download files" New topic
Author

Using wget from java code to download files

Konstantinos Vasileiou
Greenhorn

Joined: Jul 20, 2009
Posts: 16
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

Joined: Oct 29, 2001
Posts: 8836
    
    7

Welcome to the JavaRanch.
The first thing I'd try is to read in the output stream from the process.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Konstantinos Vasileiou
Greenhorn

Joined: Jul 20, 2009
Posts: 16
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."
 
 
subject: Using wget from java code to download files
 
Similar Threads
scripting help needed!
Resume download using ftp url connection
FTP'ing files onto a UNIX machine using JAVA
FTP'ing files into a UNIX machine using JAVA
How download file from http in .bat file?