1

I'm trying to access System property variable in java properties file. I'm not in the position to use other java class.

I want to define a property for a working directory(say work.dir=/home/username/working-directory), for my production .properties file, without hard-coding the /home/username. I want to reference the system property user.home in place on the hard-coded /home/username, to make work.dir more generic.

David Brossard
  • 13,584
  • 6
  • 55
  • 88
  • 3
    Neither the properties file format nor the Properties class has any native support for that. But nothing forbids you to use something like {user.home}/working-directory, and to replace {user.home} by the value of the user.home System property. – JB Nizet Jul 12 '19 at 19:16
  • Thanks for replying. I'm not clear when you said nothing forbids me to use {user.home}/working-directory. This is calling system property if I am correct – Vinayak Shastri Jul 12 '19 at 19:24
  • No. Read my comment again. Nothing will replace {user.home} (or any other convention you choose) by /home/username. You need to do that by yourself. – JB Nizet Jul 12 '19 at 19:28
  • 1
    If you're fine with build-time interpolation and you're using Maven, have a look at [resource filtering](https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html). Don't know what your exact use case is, so I'm not sure if it works for you. – crizzis Jul 12 '19 at 19:33
  • Possibly related: https://stackoverflow.com/questions/35388882/find-place-for-dedicated-application-folder – VGR Jul 12 '19 at 20:47

1 Answers1

0

This will be used to get the system variable -

System.getenv("EnviromentVariable");

and the above value you can set the java Properties key.

Alternate way, you can run the jar

java -jar jarName -DpropertyName=value
System.getProperty("propertyName"); // you can set this value in java properties.
prashantr
  • 20
  • 5