Search code examples
javaappletjapplet

Java Applet : inetAddress.getHostName() throws NullPointerException


I am trying to get the hostname. inetAddress.getHostName() works in Java Applications, but in the applet, it throws java.lang.NullPointerException.

How can I get Host-Name in an applet?


Solution

  • java.net.InetAddress.getLocalHost().getHostName()
    

    and

    java.net.InetAddress.getLocalHost().getHostAddress()
    

    work in applets, only when the applet is signed. To sign, one can use jarsigner utility, which is included with the JDK.

    keytool -genkey -alias signFiles -keystore <gen_file> -keypass <key>-dname "cn=<somename>" -storepass <password>
    
    jarsigner -keystore <gen_file> -storepass <password> -keypass <key> -signedjar <signed>.jar <unsigned>.jar signFiles
    

    All values within <...> must be replaced by appropriate values.