简体   繁体   中英

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?

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()

Java system properties have nothing to do with shell environment variables.

You can assign a java system property when you invoke the virtual machine, for example:

java -DTOOLS_DIR=/somewhere org.example.MyClass

Try this instead:

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

The getProperty(...) method returns java vm properties (like user.dir, java.version). The getenv(...) method is for environment variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM