If I run the following code
public class NetworkTester {
public static void main( String[] args ) {
System.setProperty( "java.net.preferIPv6Addresses", "true" );
System.setProperty( "java.net.preferIPv4Stack", "false" );
try {
InetAddress addr = InetAddress.getByName( "www.google.com" );
System.out.println( addr );
if ( addr instanceof Inet4Address ) {
System.out.println( 4 );
}
if ( addr instanceof Inet6Address ) {
System.out.println( 6 );
}
} catch ( UnknownHostException e ) {
e.printStackTrace();
}
}}
I get the following output
www.google.com/2607:f8b0:4009:801:0:0:0:2004
6
however if i add the line
private static Logger log4j = LogManager.getLogger();
and i import the following
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
I get
www.google.com/4.59.40.94
4
I am using log4j2 2.6.2 and jdk1.8.0_51. If i use -Djava.net.preferIPv6Addresses=true
as a vm argument i do get a IPv6 address. But the application I'm developing is meant to be run as an executable jar file and i cannot find a way to get the executable jar to run the vm argument without writing a bat file or script of some kind.
How can i get My application to prefer ipv6 and run log4j?
Have you tried adding a static initializer block to the main class that sets the property before the static Logger field is initialized?
static {
System.setProperty("java.net.preferIPv6Addresses", "true");
}