2017-12-12 29 views
0

EAR로 패키징되어 RHEL에서 실행되는 Wildfly 10.1.0에 배포 된 직접적인 J2EE 응용 프로그램을 보유하고 있습니다. EAR에는 EJB 모듈, WAR 모듈 및 기타 종속성과 함께 EAR의/lib 폴더에 상주하는 공유 라이브러리 모듈 (Commons-1.0-SNAPSHOT.jar)이 들어 있습니다. Commons-1.0-SNAPSHOT.jar의 루트에는 유틸리티/헬퍼 클래스 (즉, cc.iapps.sprd.commons.Utility)가 읽는 속성 파일 (util.properties)이 있습니다.이 유틸리티는 동일한 항아리에 패키지되어 있습니다. . 워해머 모듈은 유틸리티 클래스를 사용하지만이 클래스를 초기화 할 때, 등록 정보 파일은 다음과 같은 오류와 함께로드 실패 :WAR 모듈의 EAR/lib jar에 포함 된 액세스 등록 정보 파일

특성 파일을 찾을 수 없습니다

: java.io.FileNotFoundException :/내용/(해당 파일이나 디렉토리가 없음)

유틸리티 클래스가로드되어 있으므로 Commons-1.0-SNAPSHOT.ear/lib/Commons-1.0- SNAPSHOT.ear/SNAPSHOT.jar은 WAR의 클래스 경로에 있습니다. 또한 속성 파일이 jar 파일의 루트에 있고 jar 파일이 EAR의/lib 폴더에 있는지 확인했습니다. 이상한 무엇

ClassLoader classLoader = this.getClass().getClassLoader(); 
File file = new File(classLoader.getResource("util.properties").getFile()); 
     Properties props = new Properties(); 
     props.load(new FileInputStream(file)); 

내 개발 컴퓨터에 로컬로 10.1 제이보스 이클립스에서 배포 할 때 응용 프로그램이 잘 작동한다는 것입니다 다음과 같이

내가 속성 파일을로드하는 데 사용하는 코드입니다. 로컬 버전이 내 개발 파일 구조를 참조하는 분해 된 EAR로 배포 되었기 때문에 그것이 의심 스럽습니다.

답변

0

리소스 이름이 너무 해결 된 다음 그것으로 코드를 변경, '/'로 시작해야하는 것 같다

ClassLoader classLoader = this.getClass().getClassLoader(); 
File file = new File(classLoader.getResource("/util.properties").getFile()); 
Properties props = new Properties(); 
props.load(new FileInputStream(file)); 
1

당신은 일반적으로 java.io.File로 클래스 로더 자원을 읽으려고해서는 안된다 사물. 폭발 전개에 수행하지 않는 한 파일 시스템에 존재하지 않습니다.

당신이 아래로 축소 할 수 제공하는 솔루션 : 더 제대로

ClassLoader classLoader = this.getClass().getClassLoader(); 
Properties props = new Properties(); 
props.load(classLoader.getResourceAsStream("/util.properties")); 

나 :

ClassLoader classLoader = this.getClass().getClassLoader(); 
try (InputStream utilsInput = classLoader.getResourceAsStream("/util.properties")) { 
    Properties props = new Properties(); 
    props.load(utilsInput); 
    ... 
} 

적절한 자원 관리를위한.

+0

스티브, 당신이 맞습니다. 사실 그것은 경로 수정과 함께 변경되었습니다. 파일 시스템 방법을 사용하는 것은 JUnit 테스트가 항아리에 패키지되기 전에 클래스에서 실행 된 결과입니다. 나는 당신의 답을 옳은 것으로 표시 할 것입니다. –