繁体   English   中英

为什么我的Web应用程序无法访问我的.properties文件

[英]Why can't my web application access my .properties file

我已经为Web应用程序正常运行所需的几种配置创建了一个.properties文件。 我创建了一种用于访问此文件及其属性的测试方法,但也无法使其正常工作。 在我的Web应用程序类和测试方法中,我都遇到相同的null指针异常,该异常将带您浏览工厂类和控件类的列表,但问题都出在同一方法上,这里是例外:

java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.RichardDavy42.properties.Configure.getProperty(Configure.java:56)
at com.secureautodata.business.data.CustomerFactory.fetchCustomer(CustomerFactory.java:44)
at com.secureautodata.business.data.LeadAssembly.assembleLead(LeadAssembly.java:42)
at com.secureautodata.control.DirectoryAssistance.doPost(DirectoryAssistance.java:396)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Configure.java是包含我的getProperties方法的类,其中包含一些简单的代码,如下所示:

    /**
 * Calls on the properties file to fetch the properties that 
 * you are looking for, for use in your application.
 * @param propertyName
 * @return
 */
public static String getProperty(String nof, String propertyName){
    String property = "";

    Properties props = new Properties();

    try (InputStream input = Configure.class.getClassLoader().getResourceAsStream(nof)){

        props.load(input);
        property = props.getProperty(propertyName);

    } catch (IOException ioe) {
        System.err.println("There was an Error retrieving your data");
        ioe.printStackTrace();
    } 
    return property;
}

我尝试用不同的方法来多次调用此文件,例如:

System.out.println("Configure Properties property retrieval " + Configure.getProperty("/WebAppRootFolder/config.properties", "enc1"));

System.out.println("Configure Properties property retrieval " + Configure.getProperty("WebAppRootFolder/config.properties", "enc1"));

System.out.println("Configure Properties property retrieval " + Configure.getProperty("/config.properties", "enc1"));

System.out.println("Configure Properties property retrieval " + Configure.getProperty("config.properties", "enc1"));

所有这些给了我相同的异常,涉及到getProperty方法,因此我认为我的文件可能为空,我打开了文件,发现文件不包含此信息:

#Mon Jan 19 13:32:58 EST 2015
enc2=aijf3w97fhfieu4bfqg8o34fibagc48wfbhhba4
enc1=sldhnf9aw7h3un429q8anf9478wfi9a7hw49hiu

我在引用的任何地方都使用了相同的方法,因此我在所有级别上都尝试过。

文件结构如下:

WebAppRootFolder
|
|_src
|_TomcatLibrary
|_JUnitLibrary
|_Maven
|_Referenced
|_JRE
|_build
|_docs
|_target
|_webcontent
|_config.properties <--- config.properties in root folder
|_javadoc.xml
|_LICENSE
|_pom.xml
|_README.md

我唯一能想到的是,Configure类所在的PropertiesConfiguration是我创建的较早项目的一个jar,也许它正在尝试在其内部查找文件,但是我不确定这是否是案件。

如果有帮助(在这种情况下,我不确定是因为我在main方法中遇到的错误与在其他地方一样),这是原始方法调用的流程:

User calls request on a file -->
Servlet Instantiates Factory Class -->
Factory method makes use of Config.getProperty() During file call-->
Factory method Creates Object and returns the Object to the User --X   

谢谢大家的帮助。

为了回答这个问题,我在@cool的帮助下提供了一些我们分享的评论,所以可以这样:

我使用的Configure.getProperties()方法是我在较早项目中创建的jar文件的一部分。 因此,每次调用该方法时,它都会在该jar文件中寻找.properties文件。 为了找到此错误,我将.properties文件移到了先前的项目中,然后重新部署了jar文件。 这样做之后,我再次测试了我当前的项目,中提琴就像它起作用的魅力一样。

因此,如果将来您碰巧要构建一个需要jar文件的项目,而该jar文件需要调用.properties文件来运行您正在构建的应用程序,则该事件发生。 .properties文件必须与jar文件一起部署,或者至少必须有一种方法可以从当前应用程序中进入该项目。

我希望这有助于解决这个问题。

暂无
暂无

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

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