简体   繁体   English

System.getenv 仍会在 MacOS Mojave 上获取已删除的 env 变量值 - Java

[英]System.getenv still picks up deleted env variable value on MacOS Mojave - Java

I added some exportable environment variables in my bash_profile and my profile files in the following format export "X=y".我在我的 bash_profile 和我的配置文件中添加了一些可导出的环境变量,格式为 export "X=y"。 This worked as it should, now I want to remove them permanently .这应该有效,现在我想永久删除它们。 I've tried the following methods:我尝试了以下方法:

  1. Deleting the exported variables from the bash_profile and the profile files, and saving the files, and calling source ~/.bash_profile and source ~/.profile.从 bash_profile 和配置文件中删除导出的变量,并保存文件,并调用 source ~/.bash_profile 和 source ~/.profile。
  2. Called "unset X" in terminal window.在终端窗口中称为“unset X”。 Before I could call "echo $X" which would display y, now it displays nothing.在我可以调用将显示 y 的“echo $X”之前,现在它什么都不显示。
  3. Rebooting the Mac Computer.重新启动 Mac 计算机。

Still however, whenever I call Java's System.getenv(X), env variable X's value (y) is still returned.然而,每当我调用 Java 的 System.getenv(X) 时,仍然返回环境变量 X 的值 (y)。 What else do I need to do to completely eliminate an env variable from my system in MacOS Mojave?我还需要做什么才能在 MacOS Mojave 中从我的系统中完全消除环境变量?

It appears you believe environment variables are global.您似乎认为环境变量是全局的。 That is, modifying the value in one program, such as a running terminal, will affect the value in a different program.也就是说,修改一个程序(例如正在运行的终端)中的值将影响另一个程序中的值。 That is not how env vars work in a UNIX like OS.这不是环境变量在像操作系统这样的 UNIX 中的工作方式。 Each process is provided a copy of the env vars provided by the parent process.每个进程都提供了父进程提供的环境变量的副本。 That is, they are inherited from the parent process.也就是说,它们是从父进程继承的。

The fact that you have to unset X in a terminal to remove it means that either it is being inherited by the shell from the terminal process or your shell is setting it.您必须在终端中unset X才能将其删除这一事实意味着它是由 shell 从终端进程继承的,或者您的 shell 正在设置它。 In the latter case the specific files read when a shell starts depends on the shell.在后一种情况下,shell 启动时读取的特定文件取决于 shell。 But /etc/profile is read by most interactive shells so you might want to look there.但是/etc/profile被大多数交互式 shell 读取,因此您可能想要查看那里。

You say your Java app is run by Tomcat but failed to mention how Tomcat is started.您说您的 Java 应用程序由 Tomcat 运行,但没有提及 Tomcat 是如何启动的。 That is important for the reasons I mention above.由于我上面提到的原因,这很重要。

Note that macOS uses a daemon named launchd to manage running most services.请注意,macOS 使用名为launchd的守护程序来管理运行的大多数服务。 Those services are configured via "plist" files.这些服务是通过“plist”文件配置的。 See man launchd.plist .参见man launchd.plist Those launchd config files support defining custom env vars.这些launchd 配置文件支持定义自定义环境变量。 That is slightly unusual but worth looking at.这有点不寻常,但值得一看。 See if any of the files in ~/Library/LaunchAgents , /Library/LaunchAgents , or /Library/LaunchDaemons mention the env var.查看~/Library/LaunchAgents/Library/LaunchAgents/Library/LaunchDaemons 中的任何文件是否提到了 env var。

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

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