Check if class exists in Java classpath without running its static initializer?

JavaClassClassloader

Java Problem Overview


If I use

   try {
      Class.forName("my.package.Foo");
      // it exists on the classpath
   } catch(ClassNotFoundException e) {
      // it does not exist on the classpath
   }

the static initializer block of "Foo" is kicked off. Is there a way to determine whether a class "my.package.Foo" is on the classpath without kicking off its static initializer?

Java Solutions


Solution 1 - Java

Try the forName(String name, boolean initialize, ClassLoader loader) method of Class and set the param initialize to false.

JavaDoc link

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEpagaView Question on Stackoverflow
Solution 1 - JavaAndréView Answer on Stackoverflow