简体   繁体   English

使用Eclipse从java读取环境变量

[英]reading environment variables from java using Eclipse

Everywhere I search, it says you can get an environment variable by using System.getenv(str).在我搜索的任何地方,它都说您可以使用System.getenv(str).获取环境变量System.getenv(str).

It's not working for me.它对我不起作用。 Here's what I am doing: OS : Mac OS x 10.7 Java 1.6.x这是我在做什么:操作系统:Mac OS x 10.7 Java 1.6.x

If I do export abc=/hello/ in my terminal and then echo $abc , it gives me the variable.如果我在终端中export abc=/hello/然后echo $abc ,它会给我变量。 If I close the terminal, reopen it again and do echo $abc , it's gone.如果我关闭终端,再次重新打开它并执行echo $abc ,它就消失了。 To overcome this, I edited my .bash_profile file and inserted export abc=/hello/ .为了克服这个问题,我编辑了我的.bash_profile文件并插入了export abc=/hello/ Close the terminal, do echo $abc and it works.关闭终端,执行echo $abc并且它可以工作。 So I understood that the env variable is permanent now.所以我明白 env 变量现在是永久的。

Now if in my java console app, I print System.getenv("abc") , it returns null .现在,如果在我的 java 控制台应用程序中,我打印System.getenv("abc") ,它返回null What am I missing?我错过了什么?

The reason that you needed to put the export in your .bash_profile is that setting environment variables in a shell only persist the variables in that shell, and - since you used export - to children of that shell, or in other words, other programs launched by that shell. 你需要把原因export在你.bash_profile的是,在一个shell设置环境变量只能坚持在外壳中的变量,并且-因为你使用的export -到外壳的孩子 ,或者换句话说,其他程序启动通过那个壳。

If you're running your java code from Eclipse, and you launch Eclipse from a shell with your environment variables set, then your program should see the added environment variables. 如果您正在从Eclipse运行Java代码,并且从设置了环境变量的shell启动Eclipse,那么您的程序应该会看到添加的环境变量。 To launch Eclipse from the shell, you'll need to use the OS X open command: 要从shell启动Eclipse,您需要使用OS X open命令:

$ open /Applications/eclipse/Eclipse.app

Alternately, you can set the environment variables within your Eclipse project, and you'll need to do this if you're not launching Eclipse from a shell with the proper environment. 或者,您可以 Eclipse项目中设置环境变量,如果您没有从具有适当环境的shell启动Eclipse,则需要执行此操作。 In the Run Configurations dialog, look for a tab named Environment . 在“ 运行配置”对话框中,查找名为“ 环境”的选项卡。 Here you'll find a table for adding environment variables that will be passed to your program. 在这里,您将找到一个表,用于添加将传递给您的程序的环境变量。

It's better to add the environment variables to the Run Configuration since that way they'll always be available to your project. 最好将环境变量添加到运行配置中,因为这样它们将始终可用于您的项目。 Your code doesn't actually care where the environment variables are coming from, and adding them to the project is simpler, and will work the same way on different platforms. 您的代码实际上并不关心环境变量的来源,将它们添加到项目中更简单,并且在不同平台上的工作方式相同。

Of course, when you run your program outside Eclipse, you'll need to make sure that the same environment variables exist in the shell where you eg run java . 当然,当您 Eclipse 之外运行程序时,您需要确保在您运行java的shell中存在相同的环境变量。

Eclipse does not use the system's env variables unless launched directly from the shell (which is how it is generally launched, by clicking its icon). Eclipse不使用系统的env变量,除非直接从shell启动(通常是通过单击其图标启动它)。 In that case you will have to explicitly set the required env variables in the environment tab of the run configuration of the program. 在这种情况下,您必须在程序的运行配置的环境选项卡中显式设置所需的env变量。

I too faced The same issue , I resolved it this way.我也遇到了同样的问题,我是这样解决的。

  • Open Terminal打开终端

  • cd to the folder where eclipse.app is located Eg cd /Users/Shared/eclipse/jee-2020-09 cd 到 eclipse.app 所在的文件夹 例如cd /Users/Shared/eclipse/jee-2020-09

  • Type open Eclipse.app/键入open Eclipse.app/

  • Eclipse will now open and will be able to access the system environment variables as well. Eclipse 现在将打开并且也能够访问系统环境变量。

Check it using the code:使用代码检查它:

System.getenv().forEach((k, v) -> {
    System.out.println("ENV : " + k + ":" + v);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM