Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 237
Derek Avatar asked Jan 04 '11 19:01

Derek


People also ask

What does system getProperty variable return?

getProperty("path. separator"); The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.

How does system getProperty work in Java?

getProperties() fetches the current properties that JVM on your System gets from your Operating System. The current System properties are returned as Properties object for use by the getProperties() method. If no such set of properties is present, a set of system is first created and then initialized.

What is getProperty used for?

The getProperty(String key) method in Java is used to returns the system property denoted by the specified key passed as its argument.It is a method of the java. lang. System Class. where key is the name of the System property.


1 Answers

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

like image 123
Konstantin Komissarchik Avatar answered Sep 23 '22 11:09

Konstantin Komissarchik