Servlet应用程序中没有加载任何属性文件

时间:2018-01-22 10:40:50

标签: java servlets java-ee log4j

我正在准备一个servlet应用程序,并且没有任何属性文件被加载到应用程序中。即使所有属性文件都驻留在同一位置。

enter image description here

代码:

Properties prop = new Properties();
        InputStream input = getClass().getClassLoader().getResourceAsStream("ldap.properties");;
         try{
                logger.debug("Loading Properties File");
                //input = new FileInputStream("ldap.properties");

                // load a properties file
                prop.load(input);
                logger.debug("Retry Count is: " +  Integer.parseInt(prop.getProperty("retry")));
                System.out.println("Retry Count is: " +  Integer.parseInt(prop.getProperty("retry")));
                logger.debug("downTimeValue is: " +  Integer.parseInt(prop.getProperty("downTimeValue")));
                retry = Integer.parseInt(prop.getProperty("retry"));
                downTimeValue = Integer.parseInt(prop.getProperty("downTimeValue"));

            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

错误:

[#|2018-01-22T15:34:27.313+0530|SEVERE|oracle-glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=64;_ThreadName=Thread-2;|log4j:WARN No appenders could be found for logger (com.amdocs.LDAPKit).|#]

[#|2018-01-22T15:34:27.314+0530|SEVERE|oracle-glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=64;_ThreadName=Thread-2;|log4j:WARN Please initialize the log4j system properly.|#]

[#|2018-01-22T15:34:27.314+0530|SEVERE|oracle-glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=64;_ThreadName=Thread-2;|log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.|#]

[#|2018-01-22T16:02:37.033+0530|WARNING|oracle-glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=67;_ThreadName=Thread-2;|StandardWrapperValve[com.amdocs.LDAPKit]: PWC1406: Servlet.service() for servlet com.amdocs.LDAPKit threw exception
java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at com.amdocs.LDAPKit.doPost(LDAPKit.java:133)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)

1 个答案:

答案 0 :(得分:1)

我强烈建议您将项目转换为 maven项目,以便它处理依赖项并构建项目。

之后,将您的属性文件移至资源文件夹,因为getClass().getClassLoader().getResourceAsStream("ldap.properties");会在资源文件夹中查找文件。

您的项目结构应如下所示

enter image description here