6

I am trying to get a user environment variable in java.

The System.getEnv() method only return system environment variables.

Does anybody know how to get user environment variables?

6
  • Try this once Commented May 12, 2014 at 13:28
  • Yes I used System.getenv("myvariable") but it doesn't word. It only works for system variables, not user variables Commented May 12, 2014 at 13:31
  • Works just fine for me, its even case-insensitive. Note: On Windows 7. Commented May 12, 2014 at 13:39
  • Ok tks, I can't understand why it doesn't for me... My variable is named CODEAGENCE. In my programm System.getenv("CODEAGENCE") returns null. And for exemple System.getenv("CLASSPATH") returns the CLASSPATH correctly. Commented May 12, 2014 at 13:43
  • Have you tried restarting Windows? Also, when setting an environment variable you cannot necessarily reference other environment variables. How are you setting your variable? Commented May 12, 2014 at 13:46

4 Answers 4

14

When running a process there is only one environment. The user variables are merged into the system variables, with overwriting existing ones. This complete environment is then retrieved by the System.getenv() method.

So you actually see the user's environment variables.

I just tested it with these four scenarios:

1) No variable named MYVAR:

Running System.out.println(System.getenv("MYVAR")) prints out null.

2) System variable called MYVAR with value System:

Running System.out.println(System.getenv("MYVAR")) prints out System.

3) User variable called MYVAR with value User:

Running System.out.println(System.getenv("MYVAR")) prints out User.

4) System variable called MYVAR with value System and user variable called MYVAR with value User:

Running System.out.println(System.getenv("MYVAR")) prints out User.


Maybe you did try it out from an IDE (like Eclipse)? When changing the environment variables, you unfortunately have to restart Eclipse, as they are not correctly propagated to the run configurations.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to restart my VSCode, and then the environment variable was propogated to the run configurations like you said.
1

System.getenv(String) is the correct method; there are only "environment variables" - Windows applies the "system" environment variables to everyone's account, but they aren't differentiable at the application level. You can verify by opening a cmd prompt and executing set (with no arguments).

1 Comment

In a command prompt, my variable is ok. A echo %myvariableM give me the good value. But the System.getenv("myvariable") give me a null value
0

Seelenvirtuose is right. You need to write the name of the SYSTEM environment variable using quotes:

String serverUrl = System.getenv("QNAP-NAS-ADDRESS");
private String jdbcURL = "jdbc:mysql://" + serverUrl + "schema_name?useSSL=false";
private String jdbcUsername = System.getenv("QNAP-MYSQL-UN");
private String jdbcPassword = System.getenv("QNAP-MYSQL-UN");

Printing these:

System.out.println(System.getenv("QNAP-MYSQL-UN"));
System.out.println(System.getenv("QNAP-MYSQL-PW"));
System.out.println(System.getenv("QNAP-NAS-ADDRESS"));
System.out.println(System.getenv("QNAP-NOIP-ADDRESS"));

would display in terminal:

root
password
mynasserver.myqnapcloud.com
mynasserver.ddns.net

I didn't use USER VARIABLES, but SYSTEM VARIABLES.

@Seelenvirtuose Thanks, btw

Comments

-2

You can get the value of a USER environment variables, by reading the registry key in the path: HKEY_CURRENT_USER\\Environment There you will find all the USER environment variables added.

and for the SYSTEM environment variables you can read in the path: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment

1 Comment

Registry access is not a common practice and available to a java program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.