7

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

enter image description here

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.

JLT
  • 3,052
  • 9
  • 39
  • 86
  • How are you running the java application? – Azquelt Apr 16 '18 at 11:11
  • As a side note, you're setting `PATH` in your `.bash_profile` (and setting it to a literal string because you missed out a `$` before `GOOGLE`) but then you're trying to read `GOOGLE_APPLICATION_CREDENTIALS` in your `echo` command and your app. However if the `echo` command is working, I would expect your app code to also work, if it's being run in the same environment. – Azquelt Apr 16 '18 at 11:13
  • @Azquelt I am running the java app with eclipse. I printed out all environment variables using the method provided in the answer of Leo R. but couldn't see GOOGLE_APPLICATION_CREDENTIALS there, but when I echo $GOOGLE_APPLICATION_CREDENTIALS in the terminal the result come out. – JLT Apr 16 '18 at 11:37
  • That's important information, running from eclipse makes a big difference, see my answer below. – Azquelt Apr 16 '18 at 12:14
  • Can you please run your java application using cli? `java -cp YOUR_MAIN_CLASS`. – Rishikesh Darandale Apr 16 '18 at 12:15

3 Answers3

4

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.

Azquelt
  • 1,380
  • 10
  • 15
  • Eclipse may pick up your environment variables if you reboot. Worked for me. https://stackoverflow.com/questions/29782467/system-getenv-returns-null-when-the-environment-variable-exists – Noumenon Dec 13 '19 at 18:48
  • as I can see, we have to add case by case for each environment variable, is there anyway to import them from a file ? – ktcl Dec 12 '22 at 06:18
1

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.

Abhijeet Kale
  • 379
  • 3
  • 13
  • Hi, there's program arguments and VM arguments, how should I set them? Could you provide a sample? Thanks! – JLT Apr 16 '18 at 11:40
  • @final static Please refere https://stackoverflow.com/questions/35629641/how-to-set-google-application-credentials-for-google-compute-engine?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa this link !! hope this will helpful – Abhijeet Kale Apr 16 '18 at 12:04
0

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");
Léo R.
  • 2,620
  • 1
  • 10
  • 22
  • Hi, I have printed all the variables, but GOOGLE_APPLICATION_CREDENTIALS is not in the list. So, where exactly should I set this variable, so that java can read it? – JLT Apr 16 '18 at 09:21