简体   繁体   English

Springboot无法访问从属jar的类路径中的资源

[英]Springboot Unable to access a resource inside a dependent jar's classpath

I have a spring boot JAR MyMain.jar, which has dependent jars inside BOOT-INF/lib. 我有一个弹簧启动JAR MyMain.jar,它在BOOT-INF / lib中有依赖的jar。

I am trying to access a property file inside the BOOT-INF/lib/MyDep.jar/abcd.properties. 我正在尝试访问BOOT-INF / lib / MyDep.jar / abcd.properties中的属性文件。

I tried the below code. 我尝试了以下代码。

InputStream in = new ClassPathResource("abcd.properties").getInputStream();
System.out.println("InputStream : "+in);
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(in));          
while ((line = br.readLine()) != null) {
    System.out.println(line);
}

This works perfect inside my Eclipse IDE. 这在我的Eclipse IDE内完美运行。 but when I run it on command line as jar it doesn't print anything. 但是当我在命令行中将其作为jar运行时,它什么也不会打印。

org.springframework.boot.loader.jar.ZipInflaterInputStream@214c265e org.springframework.boot.loader.jar.ZipInflaterInputStream@214c265e

readLine() gives null during command line run. 在命令行运行期间,readLine()给出null。

Could anyone please help ! 谁能帮忙!

Alternatively, It's work for me. 或者,它对我有用。

Create this class at the app project 在应用程序项目中创建此类

@Configuration
@ComponentScan("yourpackage")
public class AppConfig {
    @Configuration
    @PropertySource("common.properties")
    static class default{}
}

If you want to read a config file by different profiles (-Dspring.profiles.active) 如果要通过其他配置文件(-Dspring.profiles.active)读取配置文件

@Configuration
@ComponentScan("yourpackage")
public class AppConfig {
    @Profile("alpha")
    @Configuration
    @PropertySource("common-alpha.properties")
    static class Alpha{}

    @Profile("staging")
    @Configuration
    @PropertySource("common-staging.properties")
    static class Staging{}

    @Profile("production")
    @Configuration
    @PropertySource("common-production.properties")
    static class Production{}
}

You can use spring @Autowired annotation as below, but make sure you annotate your class with @Component or similar ones. 您可以按如下方式使用spring @Autowired批注,但请确保使用@Component或类似的类对类进行批注。

@Autowired
Environment env;

You can get the property in your properties file 您可以在属性文件中获取属性

 env.getProperty("property")

i hope it helps. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 jar 中的类路径加载资源 - Load resource from classpath in inside jar 如果springboot应用程序打包为jar,则Thymeleaf无法访问模型属性 - Thymeleaf is unable to access model attribute if springboot application is packaged as a jar 错误:无法访问 jarfile auth-api.jar - SpringBoot - Error: Unable to access jarfile auth-api.jar - SpringBoot 访问父springboot项目中JAR中存在的javascript文件 - Access javascript files present inside JAR in parent springboot project 构建后将 jar 添加到 springboot jar 的类路径的策略 - Strategies for adding a jar to the classpath of a springboot jar AFTER construction 作为 jar 运行时找不到类路径资源 - Classpath resource not found when running as jar 无法在 docker 容器内以非阻塞方式读取类路径资源文件 - Unable to read classpath resource file in non-blocking way inside docker container 通过应用服务器上的胖 jar 文件运行 springboot 应用程序时无法访问 Actuator - Unable to access Actuator when a springboot application is running via a fat jar file on an application server 如何从罐子访问ClassPath资源? - How to access a ClassPath ressource from a jar? SpringBoot 完全可执行的 jar,内部没有依赖项 - SpringBoot fully executable jar without dependencies inside
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM