Canonical file path in java - optimization problem?

My file structure has a symlink to the directory / home / me / myDir → / some / other / dir. This link is updated by another process and notifies my process. After notification, I try to get a new canonical path:

public static String getPath() { File file = new File("/home/me/myDir"); if(file.exists()) { try { String canonical = file.getCanonicalPath(); return canonical; } catch ... } 

}

The problem is that after the link has been changed (I checked its changes), it takes 3-5 times to call the above getPath () method to actually get the new path before the previous path is returned. The only thing I can think of is that java can optimize this method and return the old path. Any ideas or insights are greatly appreciated.

+7
source share
1 answer

Try disabling the Java canonicalization cache. This can be done by setting the system properties sun.io.useCanonCaches and sun.io.useCanonPrefixCache to false.

By default, canonical file names are cached for 30 seconds (read from the source here ).

+12
source

All Articles