【问题标题】:None of the properties file is getting loaded in Servlet Application [duplicate]Servlet应用程序中没有加载任何属性文件[重复]
【发布时间】:2018-06-30 23:16:17
【问题描述】:

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

代码:

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)

【问题讨论】:

    标签: java servlets jakarta-ee log4j


    【解决方案1】:

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

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

    你的项目结构应该是这样的

    【讨论】:

    • 谢谢!它就像魅力一样。
    • 不客气,很高兴它有帮助:)
    猜你喜欢
    • 2012-09-12
    • 2013-06-18
    • 2015-01-10
    • 1970-01-01
    • 2016-01-31
    • 2013-09-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多