Skip to content

Instantly share code, notes, and snippets.

@elifarley
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elifarley/748a7c38f5e9c13dcb76 to your computer and use it in GitHub Desktop.
Save elifarley/748a7c38f5e9c13dcb76 to your computer and use it in GitHub Desktop.
package java.net;
public class GetLocalHostInfo {
public static InetAddressImpl impl;
public static void main( final String[] args ) {
try {
InetAddress.getLoopbackAddress();
System.out.println( "java.version : " + System.getProperty( "java.version" ) );
System.out.println( "preferIPv4Stack : "
+ System.getProperty( "java.net.preferIPv4Stack" ) );
impl = InetAddressImplFactory.create();
System.out.println( "isIPv6Supported : " + InetAddressImplFactory.isIPv6Supported() );
getInfo();
} catch ( final UnknownHostException e ) {
e.printStackTrace();
} finally {
System.out.println( "-------------------------------------------------" );
}
}
static void getInfo() throws UnknownHostException {
try {
System.out.println( "InetAddressImpl : " + impl.getClass().getName() );
System.out.println( "getLocalHostName: " + impl.getLocalHostName() );
System.out.println( "getLocalHost : " + InetAddress.getLocalHost() );
} catch ( final Exception e ) {
e.printStackTrace();
}
}
}
#!/bin/sh¬
¬
(cd /path/to/this/proj && /java/jdk-${1:-8}/bin/java \¬
-Djava.net.preferIPv4Stack=${2:-false} \¬
-Xbootclasspath/p:. java.net.GetLocalHostInfo )¬
$ ~/GC-4380/getlocalhostinfo.sh 8
java.version : 1.8.0_45
preferIPv4Stack : false
isIPv6Supported : true
InetAddressImpl : java.net.Inet6AddressImpl
getLocalHostName: mybox
getLocalHost : mybox/185.69.214.74
-------------------------------------------------
$ ~/GC-4380/getlocalhostinfo.sh 7
java.version : 1.7.0_45
preferIPv4Stack : false
isIPv6Supported : true
InetAddressImpl : java.net.Inet6AddressImpl
getLocalHostName: mybox
getLocalHost : mybox/185.69.214.74
-------------------------------------------------
$ ~/GC-4380/getlocalhostinfo.sh 7 true
java.version : 1.7.0_45
preferIPv4Stack : true
isIPv6Supported : false
InetAddressImpl : java.net.Inet4AddressImpl
getLocalHostName: mybox
getLocalHost : mybox/185.69.214.74
-------------------------------------------------
$ ~/GC-4380/getlocalhostinfo.sh 8 true
java.version : 1.8.0_45
preferIPv4Stack : true
isIPv6Supported : false
InetAddressImpl : java.net.Inet4AddressImpl
getLocalHostName: 185-69-214-74.ded.intelignet.com.br
java.net.UnknownHostException: 185-69-214-74.ded.intelignet.com.br: 185-69-214-74.ded.intelignet.com.br: unknown error
at java.net.InetAddress.getLocalHost(InetAddress.java:1483)
at java.net.GetLocalHostName.getInfo(GetLocalHostName.java:35)
at java.net.GetLocalHostName.main(GetLocalHostName.java:18)
Caused by: java.net.UnknownHostException: 185-69-214-74.ded.intelignet.com.br: unknown error
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:907)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1302)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment