4

My java application needs to resolve environment variables in file paths at runtime , file paths will be specified in properties file as below in case of windows it will be %JAVA_HOME%\certs\myselffign.cer in case of unix it will be $JAVA_HOME\certs\myselffign.cer

My java apps needs to resolve those file path to absolute paths and load the certificates into truststore.

Is there any way to this. As of now i am checking os.name , and if os.name is windows then pattern matching for %% and using system.getenv , in case of non windows looking for $.

I am hoping there is a better way to do this

1
  • 2
    System.getEnv() is how you do it. What's your question? Commented Feb 11, 2012 at 11:48

1 Answer 1

5

You could use the standard (Java) template syntax of ${java.home} in your property files, then replace it at Runtime with the value of System.getProperty("java.home");. So in your file instead of:

certificate=%JAVA_HOME%\certs\myselffign.cer (Windows)
certificate=$JAVA_HOME\certs\myselffign.cer (*nix)

Just use a standard:

certificate=${java.home}/certs/myselfsign.cer

And in code do something like:

String javaHomePath = System.getProperty("java.home")l
Properties props = Properties.load( ...);
String certFilePath = props.get("certificate");
certFilePath = certFilePath.replaceAll("${java.home}", javaHomePath);

Mind you, using the standard template syntax also allows you to use some of the open source property replacement tools out there. Like Spring PropertyResolver. Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.