如何为Java设置环境变量 [英] How to set environment variables for Java

查看:124
本文介绍了如何为Java设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.bash_profile

export GOOGLE_APPLICATION_CREDENTIALS=/Users/jun/Downloads
export PATH=$PATH:GOOGLE_APPLICATION_CREDENTIALS

并且更改确实生效

但是,当我尝试使用以下方法访问环境变量时

However, when I try to access the environment variable with the following method

System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));

结果为 NULL

那是为什么?

注意:该应用程序是在Eclipse中运行的.

Note: The application is ran with Eclipse.

推荐答案

我怀疑您在.bash_profile中设置的环境变量没有被获取.

I suspect that the environment variable you're setting in your .bash_profile isn't getting picked up.

如果您是从 Eclipse 运行的,则需要在运行配置"的环境"选项卡上手动设置环境变量.

If you're running from Eclipse, you need to set environment variables manually on the Environment tab in the Run Configuration.

转到Run-> Run Configurations...,在Java Applications下找到或创建应用程序的运行配置,转到Environment选项卡,然后在其中添加所需的环境变量.

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.

单击Run按钮,程序将按预期打印环境变量.

Click the Run button and your program should print the environment variable as expected.

这篇关于如何为Java设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆