简体   繁体   中英

ClassLoader failed locating my properties file

my problem is when i use this code in IDE such as eclipse it works well, but when i use it in tomcat server it doesn't work this is a part of java servlet application (i don't use maven or spring...)

public class DAOFactory {
    private final static String PROPERTY_DRIVER = "driver";
    private final static String PROPERTY_USER_NAME = "userName";
    private final static String PROPERTY_PASSWD = "passwd";
    private final static String PROPERTY_DB_NAME = "dbName";
    private final static String PROPERTY_URL = "url";
    private final static String FILE_PROPERTIES = "/com/DAO/dbInfos.properties";
    
    private String url;
    private String passwd;
    
    private String userName;
    
    /**
     * 
     * @param url
     * @param passwd
     * @param userName
     */
    public DAOFactory(String url,String passwd,String userName)
    {
        this.url = url;
        this.passwd = passwd;
        this.userName = userName;
    }
    /**
     * 
     * @return
     * @throws DAOConfigurationException
     */
    public static DAOFactory getInstance()throws DAOConfigurationException
    {
        Properties properties = new Properties();
        String passwd;
        String url;
        String userName;
        String driver ;

        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream input  = loader.getResourceAsStream(FILE_PROPERTIES);
        
        if(input == null)
            throw new DAOConfigurationException("properties file not exist EXCEPTION");
        try
        {
            properties.load(input);
            
            url = properties.getProperty(PROPERTY_URL);
            passwd = properties.getProperty(PROPERTY_PASSWD);
            
            userName = properties.getProperty(PROPERTY_USER_NAME);
            driver = properties.getProperty(PROPERTY_DRIVER);   
        }
        catch(IOException e)
        {
            throw new DAOConfigurationException("properties file loading EXCEPTION ",e);
        }
        
        try
        {
            Class.forName(driver);
        }
        catch(ClassNotFoundException e)
        {
            throw new DAOConfigurationException("------------- driver loading error ",e);
        }
        
        return new DAOFactory(url,passwd,userName);
    }
    
    public Connection getConnection()throws DAOConfigurationException
    {
        try
        {
            return DriverManager.getConnection(url,userName,passwd);
        }catch(SQLException e)
        {
            throw new DAOConfigurationException("connection failed to be created",e);
        }
    }
    
    /**
     * 
     * @return
     */
    public UniteDAO getUnite()
    {
        return new UniteImplDAO(this);
    }
}

always throws "properties file not exist EXCEPTION" the message in the if statement above

the hierarchy of the package is

src
 |_ com
    |_ DAO
        |_ DAOFactory
        |_ dbInfos.properties 
WEB-INF
  |_ 
      .
      .
      .

please could you help:)

the getResourceAsStream method of classloader cannot start with a leading slash.

However, that is not usually the correct gRAS method to use. What you almost always want (because the thread context loader is often not what you think it is, and can even be null ), is a class's loader:

DAOFactory.class.getResourceAsStream(FILE_PROPERTIES);

is what you really want, and this one does want that leading slash! This looks for the file you want in the same place DAOFactory.class exists on the classpath, eg in the same jar or whatnot. If you don't want to look in the place that class exists, then pass as parameter a java.lang.Class object as 'context' for where you should be looking. The thread context loader is not some 'just magically know where to look' cop-out.

Your code is also littered with errors and style faux-passes:

  1. You are not using try-with-resources to safely close your resources.
  2. You are not sticking to naming conventions. It is DaoFactory , not DAOFactory, and DaoConfigurationException , not DAOConfigurationException .
  3. All-caps has no place in an exception. It's an exception, the 'uhoh something went wrong caution, error, alarm,' nature is inherent to it. you don't need exclamation marks or all-caps to highlight this even further.
  4. Class.forName(driver); is not needed and hasn't been for 20 years.
  5. wrapping a SQLException into a DaoConfigurationException for opening a connection is incorrect: Maybe there is nothing wrong with your configuration, but the server itself is having issues (for example, it's reached its max simultaneous connections). More generally if the method's definition inherently ties itself to SQL (which does one does; what do you think a method named getConnection that returns a java.jdbc.Connection object, in class DaoFactory , does? The answer obviously includes 'SQL stuff'!) - then the method SHOULD be declared to throws SQLException , and there is no need to wrap it, especially wrap it in a type whose name may be a lie (it may not be a config problem at all).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM