Search code examples
javapropertiessystem

System.getProperty returns null for defined property


I have a property TOOLS_DIR that i exported in bash

I have the following line in my java file:

String toolsDir = System.getProperty("TOOLS_DIR");

Why is this returning null? is the a compatibility issue with linux or something?


Solution

  • Environment variables and properties aren't the same thing. If you want to pass in an environment variable as property you have to add the following to your java invocation:

    -DTOOLS_DIR=$TOOLS_DIR
    

    Alternatively, you can use System.getEnv()