讀取JAR resource文件

在以SpringBoot Base 專案, 使用multi module時, 當要讀取commons module文件, 路徑的排錯經歷.

此次測試範例專案架構

問題及分析

SRestapi 打包後, 啟動時報錯找不到 GeoLite2-Country.mmdb 這個檔案.

2022-06-09 15:59:29.944 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'fetchGeoLiteDatabaseReader'
java.io.FileNotFoundException: class path resource [GeoLite2-Country.mmdb] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/caster/Documents/workplace/splatform/S_Restapi/build/libs/SPlatform_Restapi.jar!/BOOT-INF/lib/S_Core-0.0.1-SNAPSHOT-plain.jar!/GeoLite2-Country.mmdb
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
        at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:162)
        at com.longxiang.config.CommonConfig.fetchGeoLiteDatabaseReader(CommonConfig.java:40)
        at com.longxiang.config.CommonConfig$$EnhancerBySpringCGLIB$$47c48ffc.CGLIB$fetchGeoLiteDatabaseReader$1(<generated>)
        at com.longxiang.config.CommonConfig$$EnhancerBySpringCGLIB$$47c48ffc$$FastClassBySpringCGLIB$$40fa806d.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)

但是去把SRestapi打包的Jar, 找得到這個檔案但為什麼會讀取不到呢? 待補詳細原因先附上最後解決方式

Last updated