spring java.io.FileNotFoundException 的原因:类路径资源 [services.xml] 无法打开,因为它不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7963496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Causes for java.io.FileNotFoundException: class path resource [services.xml] cannot be opened because it does not exist
提问by Vicky
I have a web-application on RAD 7.5. I am building the application using Maven and deploying on WebSphere 6.1.
我在 RAD 7.5 上有一个 Web 应用程序。我正在使用 Maven 构建应用程序并部署在 WebSphere 6.1 上。
A part of my web.xml is:
我的 web.xml 的一部分是:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:services.xml</param-value>
</context-param>
This is the only place where I am referencing services.xml.
这是我引用 services.xml 的唯一地方。
My services.xml is in folder
我的 services.xml 在文件夹中
FruitApplication/src/main/resources/services.xml
After building with maven, services.xml is in the following path in the target folder
使用maven构建后,services.xml在target文件夹下如下路径
target/FruitApplication-1.0.0/WEB-INF/classes/services.xml
On deployment, I continuously get below mentioned error. However, on building the application two three times, it disappears on its own and then reappears again haphazardly.
在部署时,我不断收到以下提到的错误。但是,在构建应用程序两次 3 次后,它会自行消失,然后又随意地再次出现。
What is the actual cause of this error ? How to solve this error ?
此错误的实际原因是什么?如何解决这个错误?
The top of my services.xml looks like:
我的 services.xml 的顶部看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
Is something needs to be changed here ???
这里有什么需要改变的吗???
Below is the complete pom.xml I am using for "maven-install":
以下是我用于“maven-install”的完整 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FruitApplication</groupId>
<artifactId>FruitApplication</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<archive>
<manifestFile>
src/main/webapp/META-INF/MANIFEST.MF
</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
... dependencies here ...
</dependencies>
</project>
I have already tried all the below three options:
我已经尝试了以下三个选项:
<param-value>classpath:services.xml</param-value>
<param-value>classpath:/services.xml</param-value>
<param-value>WEB-INF/classes/services.xml</param-value>
How I am deploying:
我是如何部署的:
I am removing the application from the server by right clicking on server and "add or remove applications"
我通过右键单击服务器并“添加或删除应用程序”从服务器中删除应用程序
I am doing a run-as "maven-clean" and then run-as "maven-install" on the application and again adding the application by right clicking on the server and "add or remove applications"
我正在以“maven-clean”身份运行,然后在应用程序上以“maven-install”身份运行,然后通过右键单击服务器并“添加或删除应用程序”再次添加应用程序
回答by dbreaux
Did you try classpath:/services.xml? The leading slash means to pull the resource from the root of the classpath. Without it, the file is expected to be in the package of whatever class is loading it (which I admit I don't know what that would be in this case).
你试了classpath:/services.xml吗?前导斜杠表示从类路径的根目录中提取资源。没有它,该文件应该位于加载它的任何类的包中(我承认我不知道在这种情况下会是什么)。
回答by mahendra
Select project where the reource files exist.Right click - Build path - configure build path - sources -Add folder - add the folder where you resource file exist.This would add the folder to you class path.
If the above project/jar is part of another WAR/EAR then select that WAR and/or EAR - properties - j2ee module dependecies and select the above project to get added to build path. Restart the server.
选择资源文件所在的项目。右键单击 - 构建路径 - 配置构建路径 - 源 - 添加文件夹 - 添加资源文件所在的文件夹。这会将文件夹添加到您的类路径。
如果上述项目/jar 是另一个 WAR/EAR 的一部分,则选择该 WAR 和/或 EAR - 属性 - j2ee 模块依赖项并选择上述项目以添加到构建路径。重新启动服务器。
回答by Rajendra
Figure out if its Websphere/RAD or a application issue?
弄清楚是 Websphere/RAD 还是应用程序问题?
First build the war file and drop it in a Tomcat container and check if you still see the error. 1. If you see then copy the services.xml to WEB-INF and WEB-INF/classes directory and see if it works. 2. Also check your CLASSPATH system environment variable, system CLASSPATH can override your application classpath.
首先构建war文件并将其放入Tomcat容器中,然后检查是否仍然看到错误。1.如果你看到然后将services.xml复制到WEB-INF和WEB-INF/classes目录,看看它是否有效。2. 同时检查你的 CLASSPATH 系统环境变量,系统 CLASSPATH 可以覆盖你的应用程序类路径。
If everything is fine with Tomcat, there is something specific to Websphere/RAD: 3. Check you websphere environment, I recall there is some option in websphere where you can set/override/prefix the classpath.
如果 Tomcat 一切正常,则有一些特定于 Websphere/RAD 的内容: 3. 检查您的 websphere 环境,我记得在 websphere 中有一些选项,您可以在其中设置/覆盖/前缀类路径。

