Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why System.getProperty doesn't return value from System variables in windows

I have this system variable

enter image description here

this code:

System.out.println(System.getProperty("area"));

returns null.

Could I get acces to system variables from java code through System.getProperty method ?

P.S enter image description here

like image 205
gstackoverflow Avatar asked Dec 26 '22 13:12

gstackoverflow


2 Answers

This is not a system variable but an environment variable. You can access these variables by using System.getenv(). System.getProperty() is used to return any variables set while starting the program. Example:

# java -Dmyproperty=myvalue MyProgram
like image 67
Manish Avatar answered May 06 '23 22:05

Manish


System#getProperty gets you the system properties.

You can access the environment variables Map through System#getenv

System.out.println(System.getenv("area"));

Should get you your results.

EDIT:

Try and run the following code and check if your required variable appears in the console or the behavior is incosistent for other defined variables:

public static void main(String[] args) {
        Set<String> envSet = System.getenv().keySet();
        for (String env : envSet) {
            System.out.println("Env Variable : " + env + " has value : "
                    + System.getenv(env));
        }

    }
like image 41
StoopidDonut Avatar answered May 06 '23 23:05

StoopidDonut