简体   繁体   English

无法在Eclipse for Java的动态Web项目中加载属性文件

[英]Not able to load properties file in Dynamic web project in Eclipse for Java

I've put the properties file in Webcontent folder of my dynamic web project. 我已经将属性文件放在动态Web项目的Webcontent文件夹中。 But, it throws null pointer exception. 但是,它将引发空指针异常。 Here is my code. 这是我的代码。

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream("Resources.properties");
    System.out.println(input);
    Properties properties = new Properties();       
    properties.load(input);     
    System.out.println("MySQL Driver :"+properties.getProperty("mysqlDriver"));

My properties file include 我的属性文件包括

mysqlDriver = com.mysql.jdbc.Driver

I'm getting error at InputStream input = classLoader.getResourceAsStream("Resources.properties"); 我在InputStream输入= classLoader.getResourceAsStream(“ Resources.properties”);时遇到错误

The error is 错误是

Feb 09, 2016 2:58:50 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DatabaseAccess] in context with path  [/iNTU] threw exception
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)

Put the "Resources.properties" file in the "src" folder of your dynamic web project (create it by right-clicking Jara Resources/src > new > general > File, named "MyResources.properties"). 将“ Resources.properties”文件放在动态Web项目的“ src”文件夹中(通过右键单击名为“ MyResources.properties”的Jara Resources / src>“新建”>“常规”>“文件”来创建它)。 The code to read the properties file should then be updated to: 然后应将读取属性文件的代码更新为:

InputStream input = classLoader.getResourceAsStream("/Resources.properties");
//If you use a subfolder of src, please update accordingly.

With that, Eclipse will know how to deploy the compiled files to the war's WEB-INF/classes along with the properties file. 这样,Eclipse将知道如何将已编译的文件以及属性文件一起部署到战争的WEB-INF / classs中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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