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.

5
  • How are you running the java application? Commented Apr 16, 2018 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. Commented Apr 16, 2018 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. Commented Apr 16, 2018 at 11:37
  • That's important information, running from eclipse makes a big difference, see my answer below. Commented Apr 16, 2018 at 12:14
  • Can you please run your java application using cli? java -cp <CLASS_APTH> YOUR_MAIN_CLASS. Commented Apr 16, 2018 at 12:15

3 Answers 3

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.

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

2 Comments

Eclipse may pick up your environment variables if you reboot. Worked for me. stackoverflow.com/questions/29782467/…
as I can see, we have to add case by case for each environment variable, is there anyway to import them from a file ?
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.

3 Comments

Hi, there's program arguments and VM arguments, how should I set them? Could you provide a sample? Thanks!
@final static Please refere stackoverflow.com/questions/35629641/… this link !! hope this will helpful
For me, this was the correct answer. I have an Ubuntu 20.04 LTS install, and I have both .bashrc and .profile in my home directory. Putting export MY_VAR=value into .profile allowed MY_VAR to show up in my Eclipse build environment.
-1

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

1 Comment

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?

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.