Java 在Maven中读取属性文件时使用空指针

Java 在Maven中读取属性文件时使用空指针,java,maven,Java,Maven,我有一个属性文件url.properties,位于maven webapp项目的src/main/resources文件夹中,我想从类中读取它。我怎么做?我尝试使用以下代码库,但出现错误 src/main/resources/url.properties TaxClient.java GenericUtils.java pom.xml 将文件名前面的“/”去掉。您不是在路径上查找文件,而是要求类加载器按名称查找资源,您的文件名为“url.properties”,而不是“/url.properti

我有一个属性文件
url.properties
,位于maven webapp项目的
src/main/resources
文件夹中,我想从类中读取它。我怎么做?我尝试使用以下代码库,但出现错误

src/main/resources/url.properties TaxClient.java GenericUtils.java pom.xml
将文件名前面的“/”去掉。您不是在路径上查找文件,而是要求类加载器按名称查找资源,您的文件名为“url.properties”,而不是“/url.properties”。

属性文件放在哪里?您应该检查
.getSystemResourceAsStream()的结果
null
——可能就是这种情况here@fge-是,我的getSystemResourceAsStream()返回null。为什么会这样?我可以用什么来代替?@Jigar-src/main/resources是的,我删除了文件名前面多余的“/”,但错误仍然存在。我猜src/main/resources不在您的类路径中。你是如何部署你的应用程序的?在你安装了maven之后,看看你的war文件,你的属性文件在那里吗?如果是,在哪里?是的,我可以在WEB-INF/classes文件夹中看到url.properties文件。当我在eclipse中运行代码时,它工作正常。您是否只有在将应用部署到容器中时才会遇到此问题?如果是,它是如何部署的?
tax.service.url=http://secdevapp11.gspt.net:8080/istore-tax-service/rest/tax
  public static String getTaxServiceUrl() {
        String taxServiceUrl = "";
        try {
            Properties props = GenericUtils.loadProperties("/url.properties");
            log.debug("props = " + props);
            taxServiceUrl = props.getProperty("tax.service.url");
            log.debug("taxServiceUrl = " + taxServiceUrl);
        } catch (IOException ioe) {
            log.debug("Failed to read properties file: url.properties");
        }
        return taxServiceUrl;
    }
public static Properties loadProperties(String fileName) throws IOException {
        Properties prop = new Properties();
        InputStream in = ClassLoader.getSystemResourceAsStream(fileName);
        prop.load(in);
        return prop;
    }
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <failOnMissingWebXml>true</failOnMissingWebXml>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>gif</nonFilteredFileExtension>
            <nonFilteredFileExtension>ico</nonFilteredFileExtension>
            <nonFilteredFileExtension>jpg</nonFilteredFileExtension>
            <nonFilteredFileExtension>png</nonFilteredFileExtension>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
        <webResources>
            <resource>
                <directory>src/main/webapp</directory>
                <filtering>true</filtering>
            </resource>
        </webResources>                    
    </configuration>
</plugin>
SEVERE: Servlet.service() for servlet [CheckoutPaymentController] in context with path [/istore-mvc2-webapp] 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.istore.utils.GenericUtils.loadProperties(GenericUtils.java:319)
    at com.istore.web.service.client.TaxClient.getTaxServiceUrl(TaxClient.java:44)
    at com.istore.web.service.client.TaxClient.getTaxAmount(TaxClient.java:19)
    at com.istore.utils.TaxCalculator.getSalesTax(TaxCalculator.java:16)
    at com.istore.web.controllers.CheckoutPaymentController.getTaxableAmt(CheckoutPaymentController.java:52)
    at com.istore.web.controllers.CheckoutPaymentController.processRequest(CheckoutPaymentController.java:72)
    at com.istore.web.controllers.CheckoutPaymentController.doPost(CheckoutPaymentController.java:175)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    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:581)
    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(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)