0

I have a spring boot JAR MyMain.jar, which has dependent jars inside BOOT-INF/lib.

I am trying to access a property file inside the 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. but when I run it on command line as jar it doesn't print anything.

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

readLine() gives null during command line run.

Could anyone please help !

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
RedGuts
  • 73
  • 6
  • I found it a bit hard to follow what you’re trying to do, but it sounds like it should work. Perhaps you can share a small sample project that reproduces the problem? – Andy Wilkinson Nov 18 '17 at 07:26
  • @AndyWilkinson - Basically from a parent jar file class, I am trying to read a property file inside a dependency jar's classpath. I will create a sample project to share – RedGuts Nov 20 '17 at 22:14

1 Answers1

0

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)

@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.

@Autowired
Environment env;

You can get the property in your properties file

 env.getProperty("property")

i hope it helps.

Panup Pong
  • 1,871
  • 2
  • 22
  • 44
  • I can give a try but I want the property source to be a argument parameter, not a hardcoded static file – RedGuts Nov 20 '17 at 22:16
  • @PropertySource("${propertiesArgument}.properties") and you just pass -DpropertiesArgument on command line parameter supplied during application launch. – Panup Pong Nov 21 '17 at 03:39