gstackoverflow
gstackoverflow

Reputation: 36984

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

Upvotes: 7

Views: 14176

Answers (3)

StoopidDonut
StoopidDonut

Reputation: 8617

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));
        }

    }

Upvotes: 3

Salah
Salah

Reputation: 8657

To list all Java System Properties Use, Then check if you have an area property:

Properties props = System.getProperties();
props.list(System.out);

See This example

Upvotes: 0

Manish
Manish

Reputation: 3968

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

Upvotes: 10

Related Questions