问题
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)
回答1:
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 downlaod the jar from maven repository: https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils
回答2:
You are getting a java.lang.NoClassDefFoundError which does NOT mean that your class is missing (in that case you'd get a java.lang.ClassNotFoundException). The ClassLoader ran into an error while reading the class definition when trying to read the class.
Put a try/catch inside your static initializer and look at the exception. If you read some files there and it differs from your local environment it's very likely the cause of the problem (maybe file can't be found, no permissions etc.).
来源:https://stackoverflow.com/questions/44802350/getting-noclassdeffounderror-when-trying-to-set-up-propertiesconfiguration