简体   繁体   English

以“ =”开头的java System.getenv环境名称

[英]java System.getenv environment names starting with “=”

I've noticed that the environment in Java on Windows (as obtained by a System.getenv() call) includes some variables that don't exist in the real environment. 我注意到Windows上Java中的环境(通过System.getenv()调用获得)包含一些在实际环境中不存在的变量。 These begin with and equals-sign and include "=ExitCode" which maps to the exit code of the process that ran just before this java invokation; 这些以和等号开头,并包含“ = ExitCode”,该映射到此Java调用之前运行的进程的退出代码。 and the default directories of various drive letters, such as "=C:", "=D:". 以及各种驱动器号的默认目录,例如“ = C:”,“ = D:”。 This seems to be the case with all of Sun's Java versions, running on all Windows versions. 在所有Windows版本上运行的所有Sun Java版本似乎都是这种情况。 Is this documented anywhere, or is it purely for Sun's internal only? 是否在任何地方都记录了此文档,或者仅用于Sun内部?

Edit Here's a simple example application to show what I mean. 编辑这是一个简单的示例应用程序,用于说明我的意思。 Compile and run this on the command line: 编译并在命令行上运行它:

import java.util.Map;
class ShowEnv {
    public static void main(String[] args) {
        for (Map.Entry v : System.getenv().entrySet())
            System.out.printf("%-23s= %.54s%n", v.getKey(), v.getValue());
    }
}

Then compare the variables with the SET command (from cmd.exe) or with similar command-line program written in C. You'll find the variables beginning with = don't exist in those: 然后将变量与SET命令(来自cmd.exe)或与用C编写的类似命令行程序进行比较。您会发现以=开头的变量在这些变量中不存在:

=ExitCode              = 00000000
=::                    = ::\
=C:                    = C:\Temp

These variables are obviously added during execution of the JVM. 这些变量显然是在JVM执行期间添加的。

System variables that start in equal sign are real. 以等号开头的系统变量是实数。 What you observe is not Java adding more environment variables; 您观察到的不是Java 添加更多环境变量; it is SET command hiding some of the variables. SET命令隐藏了一些变量。

Windows prohibits the use of equal sign in names of environment variables that users can set, thus reserving variables with = in them for internal use. Windows禁止在用户可以设置的环境变量名称中使用等号,因此保留了其中带有=变量供内部使用。 These variables can be retrieved through windows APIs , eg GetEnvironmentStringsW . 可以通过Windows API (例如GetEnvironmentStringsW 检索这些变量。 Java library does not filter this list, so the special variables become available to your code. Java库不会过滤此列表,因此特殊变量可用于您的代码。 SET command of Windows, on the other hand, filters them out, creating a discrepancy. 另一方面,Windows的SET命令会将它们过滤掉,从而产生差异。

According to this answer , these "magic" variables are there for backward compatibility with ms-dos directory handling, so you can safely ignore them. 根据此答案 ,这些“魔术”变量用于与ms-dos目录处理向后兼容,因此您可以放心地忽略它们。

Java's System.getenv() shows environment variables what Java sees. Java的System.getenv()显示了Java看到的环境变量。 If it is different from "real" environment then there difference how you run your Java and "real" environment. 如果它与“真实”环境不同,那么运行Java和“真实”环境的方式也会有所不同。

First of all what is "real" for you? 首先,什么对您来说是“真实的”? Is it cmd window? 是cmd窗口吗? Then starting cmd takes some steps (for example very obsolete but still active autoexec.bat) and then you have your variables. 然后启动cmd需要一些步骤(例如,非常陈旧但仍处于活动状态的autoexec.bat),然后便有了变量。 If "real" means for you Start->Computer->Properties->Advanced System Settings->Environment Variables then you still have to realize that there is some flow how this particular process starts and how it gets it's variables. 如果“真实”对您来说意味着“开始”->“计算机”->“属性”->“高级系统设置”->“环境变量”,那么您仍然必须意识到,该特定过程如何开始以及如何获取变量是有一定流程的。 At least you can see System Variables and User variables and understand that this is not trivial process. 至少您可以看到“系统变量”和“用户变量”,并且了解到这不是一个简单的过程。 Personally, I prefer cmd way because here I cat use command SET and see real variables for current process at current moment. 就个人而言,我更喜欢cmd方式,因为在这里我使用命令SET并在当前时刻查看当前进程的实际变量。 Current moment is another factor because variables can change in time based on some actions as well as they depend on when process started and they are preserved in this process till it dies. 当前时刻是另一个因素,因为变量可以根据某些操作随时间变化,并且取决于流程何时开始,并且在流程中保留直到死亡。

Intent of this small lecture is to show that Java process is complicated and depends on many factors and so it will be different from whatever is "real" for you. 这次小型讲座的目的是表明Java流程很复杂,并且取决于许多因素,因此它与您认为的“真实”内容有所不同。 Based on values what you provided I guess they could be some left artifacts from process how you run Java. 根据您提供的值,我想它们可能是您运行Java的过程中遗留的一些工件。 For example your run your application in Eclipse and it has its own environmental settings plus each process has its own settings too. 例如,您在Eclipse中运行应用程序,它具有自己的环境设置,而且每个进程也都有自己的设置。 Some variables may come for other variables which are used in Java. 一些变量可能来自Java中使用的其他变量。 _JAVA_OPTIONS would be good example. _JAVA_OPTIONS将是一个很好的例子。

Bottom line - if you have difference in environments - find them but don't blame Java in providing them. 底线-如果您在环境上有所不同-找到它们,但不要怪Java提供它们。 You are managing your environments, not Java. 您正在管理环境,而不是Java。

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

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