카테고리 없음

java -jar 서버 실행 FileNotFoundException 처리

have a nice day :D 2023. 1. 25. 12:45
반응형

서버 jar로 띄울 때 FileNotFoundException 에러

java -jar xxxxxx.jar

-- exception
cannot be resolved to absolute file path because it does not reside in the file system: jar:

-- 원래 소스 (오류 발생)
pom.xml

file-path: classpath:xml/

-- java

@Autowired
private ResourceLoader resourceLoader;

@Value("${file-path:''}")
private String filePath;

void reader() {
    resourceLoader.getResource(filePath + "test.xml").getFile();
    String xmlStr = FileUtils.readFileToString(file, "UTF-8");
}




-- 변경 (정상)
pom.xml

file-path: xml/

java

@Autowired
private ResourceLoader resourceLoader;

@Value("${file-path:''}")
private String filePath;

void reader() {
    ClassPathResource classPathResource = new ClassPathResource(filePath + "test.xml");
    byte[] binaryData = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
    String xmlStr = new String(binaryData, StandardCharsets.UTF_8);
}

 

반응형