There is a warning in the Java documentation for the static method URL.setURLStreamHandlerFactory that "this method can be called no more than once in a given Java Virtual Machine."
http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory)
I briefly reviewed the source code and there is one static instance variable in the URL class:
static URLStreamHandlerFactory factory;
and setURLStreamHandlerFactory just sets the factory to this variable:
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) { synchronized (streamHandlerLock) { if (factory != null) { throw new Error("factory already defined"); } SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkSetFactory(); } handlers.clear(); factory = fac; } }
Allowing this method to be called multiple times will overwrite this factory instance variable, but I cannot see why Java wants to prevent this behavior.
WHY does Java require this method to be called only once in the JVM?
source share