我遇到此问题,如本主题所述。 我打印了工作目录以确保在哪里运行。 我尝试了以下代码:
public static void main(String[] args) {
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
//load the spring configuration file
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
...
}
,然后将xml放在上面的第一次和第二次尝试的项目根目录和目标目录中,并将xml放在第二次和第三次尝试的src目录中(即使我没有这样做;但我认为这不是正确的位置,但是所有目前留给我的是可以向任何方向射击。
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at ttt.springdemo.HelloSpringApp.main(HelloSpringApp.java:14)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 13 more
这是manifest.mf文件:
Manifest-Version: 1.0
Built-By: TalT
Class-Path: lib/spring-core-5.0.8.RELEASE.jar lib/spring-jcl-5.0.8.REL
EASE.jar lib/spring-context-3.0.2.RELEASE.jar lib/spring-aop-3.0.2.RE
LEASE.jar lib/aopalliance-1.0.jar lib/spring-beans-3.0.2.RELEASE.jar
lib/spring-expression-3.0.2.RELEASE.jar lib/spring-asm-3.0.2.RELEASE.
jar
Created-By: Apache Maven 3.5.4
Build-Jdk: 1.8.0_171
Main-Class: ttt.springdemo.HelloSpringApp
请告知。 谢谢。
答案 0 :(得分:1)
该错误非常明显:找不到文件applicationContext.xml
。这意味着它不存在或不在正确的位置。
可能性:
java -jar your.jar
启动应用程序,则文件必须位于your.jar
文件的根目录中java -cp a/directory my.Application
启动,则文件必须位于a/directory
目录中。random/directory
中有一些资源文件,则需要使用java -cp randaom/directory:...other_classpath... your.Application ...
启动应用程序。如果random/direction
与工作目录相同,则可以不是.
。答案 1 :(得分:0)
输入正确的路径
ApplicationContext context = new ClassPathXmlApplicationContext("packageName/config.xml");