javanoclassdeffounderror

Getting NoClassDefFoundError when trying to set up PropertiesConfiguration


I am trying to set up a FileBasedConfigurationBuilder so that I can work with a PropertiesConfiguration but I am getting a NoClassDefFoundError. Here is my code

public class Config {
  private static Properties properties;
  private static PropertiesConfiguration config;

  public static void setUp(String path) throws ConfigurationException, IOException {
    if (config == null) {
      FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
          new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
          .configure(new Parameters().properties()
              .setFileName("myConfig.properties")
              .setThrowExceptionOnMissing(true)
              .setListDelimiterHandler(new DefaultListDelimiterHandler(','))
              .setIncludesAllowed(false));

      config = builder.getConfiguration();

      File file = new File(path);
      FileReader reader = new FileReader(file);
      config.read(reader);
    }
  }
}

And stack trace:

java.lang.NoClassDefFoundError: org/apache/commons/beanutils/BeanIntrospector
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.sun.proxy.$Proxy38.<clinit>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
    at org.apache.commons.configuration2.builder.fluent.Parameters.createParametersProxy(Parameters.java:294)
    at org.apache.commons.configuration2.builder.fluent.Parameters.properties(Parameters.java:245)

Solution

  • It seems you miss the apache commons bean utils jar (which contains the BeanIntrospector class) from your class path (commons-beanutils), make sure to add it to fix the issue.

    You can download the jar from maven repository: https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils