简体   繁体   English

java getenv Windows系统环境变量

[英]java getenv Windows system environment variable

I have a JAVA application that gets a system environment variable using System.getenv("MY_VAR"). 我有一个Java应用程序,它使用System.getenv(“ MY_VAR”)获取系统环境变量。 Is there a way to only search the Windows System environment variables and not the User environment variables similar to how we can pass an environment variable target into the C# Environment.GetEnvironmentVariable() method? 有没有一种方法可以搜索Windows系统环境变量,而不能搜索用户环境变量,这类似于我们如何将环境变量目标传递到C#Environment.GetEnvironmentVariable()方法中? Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

There is no way to do that in Java, at least not natively (see below). 在Java中,没有办法做到这一点,至少不是本机的(见下文)。

Windows merges system and user environment variables and passes the merged environment to applications, with no information about where each specific variable came from. Windows合并系统和用户环境变量,并将合并的环境传递给应用程序,而没有有关每个特定变量来自何处的信息。 C# can do it because C# does it not by accessing the process's environment, but instead accessing system environment configuration directly in the registry. C#之所以可以这样做,是因为C#并非通过访问进程的环境来执行此操作,而是直接在注册表中访问系统环境配置。 Hence, it can make that distinction. 因此,可以进行区分。 Java only gives you direct access to the equivalent of the Process environment target in C#. Java仅使您可以直接访问C#中等效于Process环境目标的内容。

Now, if you're really desperate, you could read the values from the Windows registry just as C# does . 现在,如果您真的很绝望,则可以像C#一样从Windows注册表中读取值。 The problem is that Java does not give direct access to the registry, you can only access a limited subset of registry keys through the Preferences API. 问题在于Java不能直接访问注册表,您只能通过Preferences API访问有限的一部分注册表项。 For full access you need to use JNI. 要进行完全访问,您需要使用JNI。

There is a JNI registry library at http://www.trustice.com/java/jnireg/ . http://www.trustice.com/java/jnireg/中有一个JNI注册库。 You can use this to read the same registry keys that C# uses and access the same data. 您可以使用它来读取C#使用的相同注册表项并访问相同的数据。

The take home point here is that C# gives you access to that info not through the actual environment block, but by reading system settings directly, which Java cannot do natively. 这里的要点是C#使您可以通过实际环境块访问Java信息,而不是通过实际的环境块来访问,而是通过直接读取系统设置来访问。

By the time your process receives the environment, it has all been merged into a single list of key/value pairs. 在您的流程收到环境时,所有环境都已合并为一个键/值对列表。 The separation into System and User variables is a Windows-only subtlety that does not propagate down to programs... unless you write non-portable code to read the Windows registry directly. 将System和User变量分开是仅Windows的细微之处,它不会传播到程序中……除非您编写不可移植的代码以直接读取Windows注册表。

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

相关问题 以“ =”开头的java System.getenv环境名称 - java System.getenv environment names starting with “=” Java System.getenv 不解析 docker-compose 中定义的环境变量 - Java System.getenv does not resolve environment variable defined in docker-compose TOMCAT_OPTS,环境变量和System.getEnv() - TOMCAT_OPTS, environment variable and System.getEnv() Java 系统环境变量 - Java System Environment Variable 我的系统Windows 10上环境变量中的java路径应该是什么 - what should be the java path in the environment variable on my system Windows 10 无法从 build.gradle 中的 System.getenv() 访问 mac 上的环境变量 - Cannot access environment variable on mac from System.getenv() in build.gradle 如何获取Spring Boot以将Java System.getenv()变量加载到Environment对象中? - How can I get Spring Boot to load the Java System.getenv() variables into the Environment object? System.getenv 仍会在 MacOS Mojave 上获取已删除的 env 变量值 - Java - System.getenv still picks up deleted env variable value on MacOS Mojave - Java JAVA中如何设置System.getenv("name")可以获取的环境变量 - How to set environmental variable that can be get by System.getenv("name") in JAVA 在ANT构建工具中设置系统变量,并使用Syste.getEnv(“ABC_HOME”)在Java中读取它 - Setting System variable in ANT build tool and read it in Java using Syste.getEnv(“ABC_HOME”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM