I am trying to read a URL into a String and it starts reading in the URL and then just stops at the same line every time and hangs. This happens with any URL and it happens when I run the program from the command line on Windows 7. When I run the same program in Eclipse it never hangs and reads in the entire website.
It always gets to “got to line 2” and inside of the while loop but never to “line 3”.
Here is the code I am using below. Is there some type of a size limit or something when doing it right through Windows on the command line?
URL link = new URL("http://www.yahoo.com");
System.out.println("got to this line 1");
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
System.out.println("got to this line 2");
//InputStream in = link.openStream();
String inputLine = "";
int count = 0;
while ((inputLine = in.readLine()) != null)
{
site = site + "\n" + inputLine;
System.out.println(inputLine);
}
System.out.println("exited the while loop.");
in.close();
System.out.println("got to this line 3");
I took that code, put it in a main method, added a
sitevariable asString site = "";, compiled it with IntelliJ, and ran it from a Windows 7 command prompt, and it worked fine:Output of the program:
Does it work the same if you comment out the line that prints out each line as its read in?