在Eclipse Dynamic Web项目中加载属性?

时间:2014-04-04 08:04:59

标签: java eclipse netbeans

代码:

public void initialize() {
    Properties p = new Properties();
    try {

        p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties"));

        classForname = p.getProperty("classForname");
        url = p.getProperty("url");
        username = p.getProperty("user");
        password = p.getProperty("password");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(classForname);
    System.out.println(url);
    System.out.println(username);
    System.out.println(password);
}

Netbean中的这段代码一直很好。

但是当我复制到eclipse时,它无法在运行项目时从属性文件中加载信息。

运行单个文件正常

我将属性文件放在默认包(Netbean)和/src/database.properties(Eclipse)中

请帮助我!!

这里有一些日志:

 Apr 04, 2014 3:47:48 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Login] in context with path [/WebBasic] threw exception
java.lang.NullPointerException: Connection is null !
    at my.Dao.ConnectDatabase.getConnect(ConnectDatabase.java:73)
    at my.Dao.UserDAO.checkUser(UserDAO.java:40)
    at my.Servlet.Login.processRequest(Login.java:46)
    at my.Servlet.Login.doGet(Login.java:86)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

调试此内容的一些提示:

  1. 不要把这么多东西放在一行:

    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties");
    p.load(stream);
    
  2. 您忘记关闭流(资源泄漏)

  3. 您忘了检查流不是null(=告诉您Java无法找到资源)

  4. 查看文件夹bin/。那里有文件database.properties吗?注意:您无法在Eclipse中执行此操作,因为IDE会隐藏该文件夹的内容。使用您的操作系统工具。

  5. 在代码中创建一个断点并逐步执行它以查看会发生什么。