thread "main" java.lang.Error: factory already defined

I Have created a Java program in which i am calling  the function
url.setURLStreamHandlerFactory(hh); i get the following error.
thread "main" java.lang.Error: factory already defined.

Can someone please suggest as to why the error is coming & how to resolve the same ?

Who is Participating?
looking at the source code of
you'll find that this error will be thrown when factory is already defined (factory != null)

public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
      synchronized (streamHandlerLock) {
            if (factory != null) {
                  throw new Error("factory already defined");
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
            factory = fac;

due to the java documentation:
This method can be called at most once in a given Java Virtual Machine.
because factory is a Class member (not instance member) declared as: static URLStreamHandlerFactory factory
can u post the code?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.