简体   繁体   English

System.getenv 没有得到 ~/.bash_profile 中定义的变量

[英]System.getenv doesn't get variables defined in ~/.bash_profile

Here is a line in file ~/.bash_profile这是文件中的一行 ~/.bash_profile

export MESSAGE="Hello World"

I want to access system variable MESSAGE in java.我想在 java 中访问系统变量MESSAGE

System.getenv("MESSAGE"); doesn't work.不起作用。

The .bash_profile file is only sourced for login shells. .bash_profile 文件仅用于登录 shell。 If your java process is spawned from a shell that is not a login shell (for example a script with a #!/bin/sh at the top) then it will not read it (though it may still inherit MESSAGE from the environment depending on how you run it).如果您的 java 进程是从不是登录 shell 的 shell 生成的(例如顶部带有#!/bin/sh的脚本),那么它将不会读取它(尽管它可能仍会从环境继承 MESSAGE 取决于你如何运行它)。

Note also that .bash_profile is also not run for interactive shells that are not "login" shells, so you can't rely on it being run even when you have a shell prompt.另请注意,对于不是“登录”shell 的交互式 shell,也不会运行 .bash_profile,因此即使有 shell 提示,也不能依赖它运行。 People usually use .bashrc, which is sourced for all interactive shells, for that purpose.为此,人们通常使用为所有交互式 shell 提供的 .bashrc。

If you want a variable to be set in all Bourne shell derivatives regardless of whether they are interactive or not, put it in both .profile and .bashrc.如果您想在所有 Bourne shell 衍生工具中设置一个变量,无论它们是否是交互式的,请将其放在 .profile 和 .bashrc 中。

I would recommend you to test if your environment variable is indeed "defined" by using echo $MESSAGE as suggested above.我建议您按照上面的建议使用 echo $MESSAGE 来测试您的环境变量是否确实是“定义的”。
In addition, changing the bash_profile file will not effect your current shell,此外,更改 bash_profile 文件不会影响您当前的 shell,
you must use "source" in order for it to effect your current shell.您必须使用“源”才能使其影响您当前的外壳。
I would also recommend reading this article about differences between bashrc and bash_profile.我还建议阅读这篇关于 bashrc 和 bash_profile 之间差异的文章。
Maybe you should define the EXPORT at bashrc?也许您应该在 bashrc 中定义 EXPORT?

This actually gets even more interesting for users who have a .profile (for the old Bourne shell), that gets read in by .bash_profile automatically (providing compatability).对于拥有 .profile(对于旧的 Bourne shell)的用户来说,这实际上变得更加有趣,它会被 .bash_profile 自动读取(提供兼容性)。 In either case, the environment variables are read in once only at login shell startup, and all subshells inherit those variables for free.在任何一种情况下,环境变量都只会在登录shell 启动时读取一次,并且所有子 shell 都会免费继承这些变量。 The .bashrc is for tty-dependent things and unheritables like functions (the old sh used $ENV, I think, if it were set, for something similar). .bashrc 用于依赖于 tty 的事物和不可继承的函数(如函数)(我认为旧的 sh 使用 $ENV,如果设置了类似的东西)。

Your use of ~/.bash_profile looks fine (although single quotes are more reliable than double quotes, which allow some substitutions).您对 ~/.bash_profile 的使用看起来不错(尽管单引号比双引号更可靠,双引号允许一些替换)。 Make sure you've logged out and back in between editing that file and trying your test, or use ". ~/.bash_profile" (no quotes and note the leading dot and space, since the dot is the command here).确保您在编辑该文件和尝试测试之间退出并重新登录,或使用“.~/.bash_profile”(没有引号并注意前导点和空格,因为点是此处的命令)。

The article at http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html covers some good things, like using ". ~/.bashrc" at then end of your ~/.bash_profile (excepth that you should use -r, not -f). http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html上的文章涵盖了一些好东西,比如在 ~/.bash_profile 末尾使用“.~/.bashrc”(除了你应该使用 -r,而不是 -f)。 The comment about using export in your .bashrc is wrong, you should not do this, for two reasons (1) an pretty insignificant performance penalty, (2) a pretty high chance that some command you execute won't get the environment variable - particularly things spawned from your window manager menus and other places where an actual command prompt didn't appear in any of their parents.关于在你的 .bashrc 中使用 export 的评论是错误的,你不应该这样做,原因有两个:(1)一个非常微不足道的性能损失,(2)你执行的某些命令很有可能不会获得环境变量 -特别是从您的窗口管理器菜单和其他任何父项中都没有出现实际命令提示符的地方产生的东西。

Lastly, make sure the $MESSAGE actually exists in your environment - look at the output of the "env" command - if it isn't there, the Java process won't see it unless it's internally creating it and storing it in its own internal copy of the environment variables.最后,确保 $MESSAGE 确实存在于您的环境中 - 查看“env”命令的输出 - 如果它不存在,Java 进程将不会看到它,除非它在内部创建它并将其存储在自己的环境变量的内部副本。

Another note, if you set env variables in the .profile, use this syntax:另请注意,如果您在 .profile 中设置环境变量,请使用以下语法:

VAR=VALUE ;变量=值; export VAR导出 VAR

...since the old sh shell doesn't support "export VAR=VALUE". ...因为旧的 sh shell 不支持“export VAR=VALUE”。 Using set -e before a bunch of these and set +e after can remove the need to use "export" at all, if I remember correctly.如果我没记错的话,在一堆这些之前使用 set -e 并在之后使用 set +e 可以完全消除使用“导出”的需要。

另一个要看的地方: /etc/environment (这可能会否决/替换通过 IDE 打开的 shell 中的 .bashrc 或 .bash_profile)

在此处输入图像描述

Variables in your bash profile only get used in the terminal session you are in, so IntelliJ won't pick them up.您的 bash 配置文件中的变量仅在您所在的终端会话中使用,因此 IntelliJ 不会选择它们。

Instead, go to Edit Configurations in your Run Configurations box, and add your environment variables in a semi-colon separated list in the environment variables box!相反,请转到Run Configurations框中的Edit Configurations ,然后将环境变量添加到environment variables框中的分号分隔列表中!

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

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