Reputation: 3172
I have inserted the following lines in .bash_profile
export GOOGLE_APPLICATION_CREDENTIALS=/Users/jun/Downloads
export PATH=$PATH:GOOGLE_APPLICATION_CREDENTIALS
and the changes did take effect
However, when I try to access the environment variable with the following method
System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
The result is NULL
Why is that?
Note: The application is ran with Eclipse.
Upvotes: 7
Views: 17944
Reputation: 1495
I suspect that the environment variable you're setting in your .bash_profile
isn't getting picked up.
If you're running from Eclipse, you need to set environment variables manually on the Environment tab in the Run Configuration.
Go to Run
-> Run Configurations...
, find or create the run configuration for your app under Java Applications
, go to the Environment
tab and add your desired environment variables there.
Click the Run
button and your program should print the environment variable as expected.
Upvotes: 4
Reputation: 389
To set an environment variable, use the command "export varname=value", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces.
You can set an environment variable permanently by placing an export command in your Bash shell startup script "~/.bashrc" (or "~/.bash_profile", or "~/.profile") of your home directory; or "/etc/profile" for system-wide operations.
If you use eclipse then you can set argument which used by jvm while running java program as
click right click on project name click Run as ----> Run Configuration --> click on argument tab from left upper corner.
Upvotes: 1
Reputation: 2698
Print environnement variable return a map so try this to print all env variable :
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
Or just one :
Map<String, String> env = System.getenv();
String value = env.get("GOOGLE_APPLICATION_CREDENTIALS");
Upvotes: 0