使用spring boot构建一个jar,其中一个类需要从类路径中读取文本文件。这是一段代码..
InputStream fp = this.getClass().getClassLoader().getResourceAsStream("install/release-2.2.0-SNAPSHOT/schema/Create-Index.xml");
问题是,fp正在返回' null',并且它的使用会抛出Null Pointer Exception。
这是jar内容..
**jar tvf target/orchestrationservice.jar | grep install**
0 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/
0 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/
0 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/predecessors/
0 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/predecessors/release-2.2.0/
0 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/schema/
633 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/predecessors/release-2.2.0/orchestration_upgrade_set_version.cql
910 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/predecessors/release-2.2.0/Upgrade-Index.xml
706 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/Prod-Index.xml
1108 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/schema/Create-Index.xml
465 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/schema/orchestration_3.0_fresh_install_schema.cql
1356 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/schema/orchestration_3.0_fresh_install_tables.cql
28 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/schema/orchestration_3.0_fresh_install_tables_data.cql
651 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/schema/orchestration_3.0_fresh_install_tables_idx.cql
296 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/release-3.0.0/schema/orchestration_3.0_set_version.cql
2135 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/StepExecutionOrder.xml
128 Fri Feb 10 19:19:06 PST 2017 BOOT-INF/classes/install/UpgradePaths.xml
在切换Spring Boot版本后,引入了BOOT-INF / classes / **。之前它只是/ classes / **,资源加载得很好。
请帮助我,如何使用BOOT-INF读取资源?
答案 0 :(得分:0)
这就是你要找的东西:
"安装/释放-2.2.0-SNAPSHOT /模式/创建-INDEX.XML"
这是你的jar文件中的内容:
" BOOT-INF /类/安装/释放-3.0.0 /模式/创建-INDEX.XML"
此外,在使用Spring时,从类路径获取文件时使用org.springframework.core.io.ClassPathResource
会更加惯用。