Reputation: 641
I have the below class which loads database details from a properties file. When the file "dao.properties" is missing the class should throw a DAOConfigurationException but instead I am getting
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError
com.clone.dao.DAOFactory.getInstance(DAOFactory.java:19)
com.clone.controller.register.doPost(register.java:35)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
com.clone.dao.DAOConfigurationException: Properties file dao.properties not found in classpath.
com.clone.dao.DAOProperties.<clinit>(DAOProperties.java:15)
com.clone.dao.DAOFactory.getInstance(DAOFactory.java:19)
com.clone.controller.register.doPost(register.java:35)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
and in subsequent requests I am getting this exeception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.NoClassDefFoundError: Could not initialize class com.clone.dao.DAOProperties
com.clone.dao.DAOFactory.getInstance(DAOFactory.java:19)
com.clone.controller.register.doPost(register.java:35)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
This is my code
package com.clone.dao;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class DAOProperties {
private static final String PROPERTIES_FILE = "dao.properties";
private static final Properties PROPERTIES = new Properties();
<b> static{
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream propertiesFile = classloader.getResourceAsStream(PROPERTIES_FILE);
if(propertiesFile==null){
throw new DAOConfigurationException("Properties file "+PROPERTIES_FILE+" not found in classpath.");
}
try {
PROPERTIES.load(propertiesFile);
} catch (IOException e) {
throw new DAOConfigurationException("Cannot load properties file '"+PROPERTIES_FILE+"'.", e);
}
}
</b>
private String specificKey;
public DAOProperties(String specificKey)
{
this.specificKey=specificKey;
}
public String getProperty(String key) throws DAOConfigurationException {
String fullKey = specificKey+"."+key;
String property = PROPERTIES.getProperty(fullKey);
if(property==null||property.trim().length()==0){
throw new DAOConfigurationException("Property '"+fullKey+"' is missing in properties file '"+
PROPERTIES_FILE+"'.");
}
return property;
}
}
Guys, please explain whats happening this is a lot confusing
Upvotes: 0
Views: 454
Reputation: 1500525
It is throwing a DAOConfigurationException
:
com.clone.dao.DAOConfigurationException: Properties file dao.properties
not found in classpath.
However, that's being wrapped up in an java.lang.ExceptionInInitializerError
because your DAOFactory
is trying to use DAOProperties
. The DAOProperties
class can't be initialized properly because the static initializer fails, and ExceptionInInitializerError
is exactly what gets thrown when an static initializer fails.
Perhaps you shouldn't be doing this within the static initializer?
Upvotes: 3