Marcus Adams
Marcus Adams

Reputation: 53870

How to set Java DNS resolver cache TTL in Web Start application?

I am able to tell the Java DNS resolver not to cache like this:

java.security.Security.setProperty("networkaddress.cache.ttl", "0");

This works just fine when the program is run directly from the JRE, however, when run from Web Start, this setting seems to be ignored, and it caches forever. I don't get a SecurityException.

However, if I set the security manager to null, the setting will work in Web Start:

System.setSecurityManager(null);

Does anybody know a way to enable this property in Web Start without turning off the Security Manager? Also, if you can simply shed more light on what's going on, I'd appreciate it.

Upvotes: 3

Views: 2797

Answers (2)

bestsss
bestsss

Reputation: 12056

The simple answer is that sun.net.InetAddressCachePolicy is initialized only once in the static{} initializer taking into account the property you specify. Webstart loader obviously uses the class, hence it doesn't take your value.

However, since the property may not be initialized you can use InetAddressCachePolicy.setIfNotSet(InetAddressCachePolicy.NEVER).

Alternatively there is a system property to add to the jnlp: < property name="com.sun.net.inetaddr.ttl" value="0" />

If it is specified already by the webstart (have not tested) you can resort only on hacking via reflection Field f= InetAddressCachePolicy.getDeclaredField("cachePolicy"); f.setAccessible(true); f.setInt(0); which may or may not work.

Good luck and happy hacking!

Upvotes: 2

DaveH
DaveH

Reputation: 7335

Are you running this with javaws? If you are, have you tried

javaws -J-Dnetworkaddress.cache.ttl=0 yourapp.jnlp

Upvotes: 1

Related Questions