Robert
Robert

Reputation: 42754

Java: Download always stalls

Some days ago I run into a severe problem that gives me a headache:

All Java based tools (Eclipse, Maven, Android SDK loader...) have problems downloading certain files: the download just stops at random.

I already described my problem at Superuser but nobody was able to help me with my problem. Therefore started to perform some tests myself and ended up with the sample code at the end of this question.

The interesting part is that the buffer size has an small influence on the problem. If I reduce the buffer size to 1024 the download completes in most cases.

Does this code only make problems on my Windows system?

To make it clear: I don't want to develop a download program with Java - therefore fixing the sample code does not help me - the code is only for demonstrating the problem.

public static void main(String[] args) {
    try {
        URL url = new URL("http://mirror.netcologne.de/maven2/com/google/android/android/2.3.3/android-2.3.3.jar");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream in = conn.getInputStream();
        byte[] buffer = new byte[10485760]; // 10MiB
        long read = 0;
        while (true) {
            int bytes = in.read(buffer, 0, buffer.length);
            if (bytes < 0)
                break;
            read += bytes;
            System.out.println("Bytes read: " + read);
        }
        conn.disconnect();
        System.out.println("Finished");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Views: 761

Answers (1)

Robert
Robert

Reputation: 42754

The problem you are facing is caused by Java 7 - in detail that to gives IPv6 a higher priority than IPv4.

This problem affects all Java based software but only occurs on some computers (may depend on the used Internet connection and/or local network components like switches, router...)

You can change it back to IPv4 as it was used in Java 6 by setting the system property java.net.preferIPv4Stack=true

Setting this property is different for each application. For Eclipse you have to set it in the eclipse.ini.

For Andoird SDK manager you have to edit the file tools\android.bat and add the parameter -Djava.net.preferIPv4Stack=true to the Java call near the end of the file.

Upvotes: 1

Related Questions