gpt4 book ai didi

java - 将属性文件添加到类路径但收到 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-02 00:59:28 25 4
gpt4 key购买 nike

我的 spring 上下文文件有这个:

<context:property-placeholder location="classpath:web.properties" />

我引用了如下键/值:

<property name="username" value="${dataSource.username}"/>

我的文件布局是这样的:

>/www/site/app.war
>/www/site/configs/web.properties

我使用以下方式运行我的应用程序:

>java -cp "/www/site/configs/*.*" -jar app.war

但是后来我得到了这个异常:

java.io.FileNotFoundException: class path resource [web.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:661)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)

即使我将文件添加到类路径中,它仍然找不到它,为什么?

更新

所以我的配置文件在这里:

/www/site/configs/web.properties
/www/site/configs/log4j.properties

我尝试过这个:

>/www/site/java -cp ".:app.war:/www/site/configs/*.*" com.abc.server.MyServer

和:

>/www/site/java -cp ".:app.war:/www/site/configs/web.properties:/www/site/configs/log4j.properties" com.abc.server.MyServer

甚至:

/www/site/java -cp ".:app.war" com.abc.server.MyServer

我得到同样的错误:

2013-04-25 01:19:28.210:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2013-04-25 01:19:28.294:INFO:oejw.WebInfConfiguration:Extract jar:file:/www/site/app.war!/ to /www/site/work/app
2013-04-25 01:19:32.814:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/www/site/app/},file:/www/site/app.war
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2013-04-25 01:19:33.585:INFO:/:Initializing Spring FrameworkServlet 'app'
2013-04-25 01:19:35.750:WARN:/:unavailable
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/web-context.xml]: Could not resolve placeholder 'dataSource.url' in string value [${dataSource.url}]
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)

我的 web.properties 有:

dataSource.url=jdbc:mysql://localhost/appdb
dataSource.username=root
dataSource.password=123

我的 web-context.xml 文件再次包含:

..
<context:property-placeholder location="classpath*:/web.properties" />

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${dataSource.url}"/>
<property name="username" value="${dataSource.username}"/>
<property name="password" value="${dataSource.password}"/>
..

我还尝试了以下变体:

<context:property-placeholder location="classpath:web.properties" />
<context:property-placeholder location="classpath*:web.properties" />
<context:property-placeholder location="classpath*:/web.properties" />

最佳答案

我也遇到过类似的问题。我不能说为什么,但我发现 -cp 参数在与 -jar 一起使用时不起作用。

您可以使用,

编辑

根据讨论和经历:http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

正确的语法应该是:

在窗口中:

java -cp "app.war;/www/site/configs/" <Main Class file name>

在 Linux 中:

java -cp "app.war:/www/site/configs/" <Main Class file name>

关于java - 将属性文件添加到类路径但收到 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16160414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com