일부 자습서를 실행하면서 Spring 프레임 워크를 실험하고 있습니다.File.exists()가 true 인 블록 내에서 FileNotFoundException이 발생했습니다.
어떤 이유인지 bean.xml이 없습니다.
EDIT # 5 : 답변에서 알 수 있듯이, 사용중인 메서드가 클래스 패스에서 파일을 찾고있을 때 절대 경로와 함께 존재하는 파일은 적합하지 않습니다. (ClassPathXmlApplicationContext())
EDIT 1 : 파일이 존재하고, 실패 코드를 타나에서 파일, I 상대 경로를 시도
if (xmlFile.exists())
블록 안에 : 코드를 읽어 PLEASE 프로젝트 폴더에있는 파일의 절대 경로를 사용하고 Beans.xml을 여러 드라이브의 루트로 이동하려고 시도했습니다.
내 당황은 FNF 예외가 나타납니다 이유를 다음 File.exists()가 true이고, 나는 만 가지고는 ... 같은 경로로 File.exists()
호출은 성공하지만, ClassPathXmlApplicationContext()
생성자는 항상 실패 하나 fileName에 문자열
도움을 주시면 감사하겠습니다.
EDIT # 2 : 나는 리눅스 머신에서 같은 코드를 시도했다. 정확히 동일한 오류가 발생합니다 (리눅스 경로 포함)
EDIT # 3 : 이제 파일을 target/classes에 복사했고 실행합니다. 이상적으로 나는 그것을 자원으로 원합니다 ... 어떻게 그 일에 착수합니까? 3.9.2.RELEASE
빌드 ID : 201,712,210,947
플랫폼 : 이클립스 Oxygen.2 (4.7.2)
나는 봄 도구 스위트를
버전을 실행하고
Windows의 경우 10
여기
내 ApplicationStart.java입니다 :
package com.example.pkg;
import java.io.File;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationStart {
public static void main(String[] args) {
//System.out.println("Working Directory = " + System.getProperty("user.dir"));
String xmlFileName = "E:\\Beans.xml";
File xmlFile = new File(xmlFileName);
if (xmlFile.exists()) {
System.out.println(xmlFile.getAbsolutePath() + " exists - Running Spring code");
ApplicationContext appCtx = new ClassPathXmlApplicationContext(xmlFileName);
Point p = (Point) appCtx.getBean("ZeroPoint");
System.out.println(p.getX());
System.out.println(p.getY());
Point p2 = (Point) appCtx.getBean("TenPoint");
System.out.println(p2.getX());
System.out.println(p2.getY());
} else {
System.err.println("File " + xmlFileName + " does not exist.");
}
}
}
내 Point.java
package com.example.pkg;
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
내 beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ZeroPoint" class="com.example.pkg.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
<bean id="TenPoint" class="com.example.pkg.Point">
<property name="x" value="10" />
<property name="y" value="10" />
</bean>
</beans>
내 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
</dependencies>
</project>
"자바 응용 프로그램으로 실행"의
출력 :
Working Directory = C:\Users\jonas\Documents\workspaces\sts_test\artifact
C:\Users\jonas\Documents\workspaces\sts_test\artifact\Beans.xml exists - Running Spring code
dec. 24, 2017 2:36:08 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org[email protected]137e0d2: startup date [Sun Dec 24 14:36:08 CET 2017]; root of context hierarchy
dec. 24, 2017 2:36:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:223)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:194)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:258)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:620)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:521)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.example.pkg.ApplicationStart.main(ApplicationStart.java:15)
Caused by: java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:177)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 13 more
편집 # 4 :
작동 코드입니다./의 SRC/메인/자원 파일을 넣고 아래 실행하는 나를 위해 작동 :
package com.example.pkg;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationStart {
public static void main(String[] args) {
// System.out.println("Working Directory = " + System.getProperty("user.dir"));
String xmlFileName = "Beans.xml";
//System.out.println(xmlFile.getAbsolutePath() + " exists - Running Spring code");
ApplicationContext appCtx = new ClassPathXmlApplicationContext(xmlFileName);
Point p = (Point) appCtx.getBean("ZeroPoint");
System.out.println(p.getX());
System.out.println(p.getY());
Point p2 = (Point) appCtx.getBean("TenPoint");
System.out.println(p2.getX());
System.out.println(p2.getY());
}
}
:'java.io.FileNotFoundException : 클래스 패스 리소스 [beans.xml 환경] '존재하지 않기 때문에 열 수 없습니다. '... – khmarbaise
코드를 읽으면 실제로 존재하지 않는 블록을 입력하는 파일을 확인합니다. – JoSSte
그러나 E : \ Beans.xml 파일이 존재한다는 사실 부적절하다. ClassPathXmlApplicationContext 생성자는 파일 경로를 필요로하지 않습니다. classpath resource의 위치를 요구합니다 (javadoc, 클래스 명, 및 에러 메세지가 모두 가리키는 것에 따라). https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/ClassPathXmlApplicationContext.html#ClassPathXmlApplicationContext-java.lang.String...- –