I am trying to read in a website and save it to a string. I’m using this code below which works perfectly fine in Eclipse. But when I try to run the program via the command line in Windows like “java MyProgram”, the program starts and just hangs and never reads in the URL. Anyone know why this would be happening?
URL link = new URL("http://www.yahoo.com");
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
//InputStream in = link.openStream();
String inputLine = "";
int count = 0;
while ((inputLine = in.readLine()) != null)
{
site = site + "\n" + inputLine;
}
in.close();
…
It could be because you are behind a proxy, and Eclipse is automatically adding settings in to configure this.
If you are behind a proxy, when running from the command prompt, try setting the
java.net.useSystemProxiesproperty. You can also manually configure proxy settings with a few network properties found here (http.proxyHost,http.proxyPort).