How can I disable IPv6 stack use for IPv4 IPs on JRE?
Asked Answered
G

3

52

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?

Gruel answered 7/8, 2012 at 16:56 Comment(0)
G
100

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 :)

Gruel answered 7/8, 2012 at 18:3 Comment(4)
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!!Sightless
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.Enriquetaenriquez
This is because of such stupidity that some people end up disabling IPv6 entirely from their whole operating system.Gabbey
Agreed. Just to mention this just changes the preference AFAIK and doesn't disable whole thing, it seems Carlos answer below does that however :)Gruel
A
46

Did you try (from the command line)

-Djava.net.preferIPv4Stack=true

or (from your code)

System.setProperty("java.net.preferIPv4Stack" , "true");
Arlindaarline answered 7/8, 2012 at 17:1 Comment(0)
C
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
Christensen answered 17/1, 2018 at 20:51 Comment(2)
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!Globuliferous
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!Christensen

© 2022 - 2024 — McMap. All rights reserved.