51

As you can see on below screenshots, eclipse and Android SDK Manager (and other Java programs) are trying to connect to a IPv4 Internet IP via IPv6 TCP/IP stack while Proxifier (a proxy manager program, nevermind) can not support that.

Eclipse failure in connecting to the InternetAndroid SDK Manager failure in connecting to the Internet

How I can disable IPv6 in Java?

Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81

3 Answers3

98

I wanted to use this for some program I hadn't control for running that Java app so ended with this _JAVA_OPTIONS=-Djava.net.preferIPv4Stack=true environment variable. (read about _JAVA_OPTIONS here)

If you are using Windows, just run this command on Windows cmd:
setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true

Thanks to Jason Nichols for reminding this JVM argument :)

Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
  • Thanks Ebrahim!! after I issued the command in Windows 8 command line, restarted Eclipse, the error from server side "Address is invalid on local machine, or port is not valid on remote machine" disappeared! Many thanks!! – Lin Song Yang Aug 01 '13 at 11:57
  • 2
    good find. it never occurred to me that an application could possibly use the v6 stack when clearly the destination host is a v4 system. sick. – lightxx Feb 13 '14 at 12:21
  • 1
    This is because of such stupidity that some people end up disabling IPv6 _entirely_ from their whole operating system. – MarcH Oct 23 '18 at 17:00
  • Agreed. Just to mention this just changes the preference AFAIK and doesn't disable whole thing, it seems Carlos answer below does that however :) – Ebrahim Byagowi Oct 24 '18 at 07:48
45

Did you try (from the command line)

-Djava.net.preferIPv4Stack=true

or (from your code)

System.setProperty("java.net.preferIPv4Stack" , "true");
Jason Nichols
  • 11,603
  • 5
  • 34
  • 53
8

Maybe a bit late but for me neither solution worked. So I included this option as it is stated here:

-Djava.net.preferIPv4Addresses=true

But also the followingI added line to the end of the file /etc/sysctl.conf

net.ipv6.conf.all.disable_ipv6 = 1

And

sudo sysctl -p

as it is explained here. In order to check that ipv6 is disabled just execute:

ip a | grep inet
Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
  • you, Sir, made my day! Finally able to start Java Springboot Tomcat App on tcp4 Port 8090 (by defalut it was started on tcp6 Port 8090). Thanks! – shosaco Dec 05 '18 at 12:05
  • Well the answers did not work for me and I wasted a lot of time to find out the final solution And that is the aim of stackoverflow right?. Really appreciated but no need thanks! – Carlos Cavero Jan 12 '19 at 13:29