简体   繁体   English

环境变量和系统属性之间的区别

[英]difference between environment variables and System properties

iam using the below link to understand environment variables and system properties.我使用以下链接了解环境变量和系统属性。

https://docs.oracle.com/javase/tutorial/essential/environment/env.html https://docs.oracle.com/javase/tutorial/essential/environment/env.html

The link says environment variables are set by OS and passed to applications.该链接说环境变量由操作系统设置并传递给应用程序。 When i fetch environment variables using System.getenv() it shows me lot of properties which i never set.当我使用System.getenv()获取环境变量时,它向我展示了许多我从未设置过的属性。 So it must be OS (im using macOS) which had set these properties.所以必须是操作系统(我使用 macOS)设置了这些属性。 Some of the properties in System.getenv() are MAVEN_CMD_LINE_ARGS , JAVA_MAIN_CLASS_1420 , JAVA_MAIN_CLASS_1430 . System.getenv()中的一些属性是MAVEN_CMD_LINE_ARGSJAVA_MAIN_CLASS_1420JAVA_MAIN_CLASS_1430

My question is why would OS would like to set the java specific properties in environment variables?我的问题是为什么操作系统要在环境变量中设置 java 特定的属性? Ideally these should be set by JVM (in System.properties() ).理想情况下,这些应该由 JVM 设置(在System.properties() )。

PS: From whatever i have read on net i understand that environment variables are set by OS and System.properties() are set by JVM PS:从我在网上读到的任何内容我都知道环境变量是由操作系统设置的,而 System.properties() 是由 JVM 设置的

Also if someone can point me to a good link on environment variable and System.properties it will be very helpful.此外,如果有人可以指出我关于环境变量和 System.properties 的一个很好的链接,那将非常有帮助。 Iam very confused between the two.我在两者之间很困惑。

Environment variables is an OS concept, and are passed by the program that starts your Java program.环境变量是一个操作系统概念,由启动 Java 程序的程序传递。

That is usually the OS, eg double-click in an explorer window or running command in a command prompt, so you get the OS-managed list of environment variables.这通常是操作系统,例如在资源管理器窗口中双击或在命令提示符下运行命令,这样您就可以获得操作系统管理的环境变量列表。

If another program starts your Java program 1 , eg an IDE (Eclipse, IntelliJ, NetBeans, ...) or a build tool (Maven, Groovy, ...), it can modify the list of environment variables, usually by adding more.如果另一个程序启动您的 Java 程序1 ,例如 IDE(Eclipse、IntelliJ、NetBeans 等)或构建工具(Maven、Groovy 等),它可以修改环境变量列表,通常是通过添加更多. Eg the environment variable named MAVEN_CMD_LINE_ARGS would tend to indicate that you might be running your program with Maven.例如,名为MAVEN_CMD_LINE_ARGS的环境变量往往表明您可能正在使用 Maven 运行您的程序。

In a running Java program, the list of environment variables cannot be modified.在运行的 Java 程序中,不能修改环境变量列表。


System properties is a Java concept.系统属性是一个 Java 概念。 The JVM will automatically assign a lot of system properties on startup. JVM 会在启动时自动分配很多系统属性。

You can add/override the values on startup by using the -D command-line argument.您可以使用-D命令行参数在启动时添加/覆盖这些值。

In a running Java program, the list of system properties can be modified by the program itself, though that is generally a bad idea.在运行的 Java 程序中,系统属性列表可以由程序本身修改,尽管这通常是一个坏主意。


1) For reference, if a Java program wants to start another Java program, it will generally use a ProcessBuilder to set that up. 1)作为参考,如果一个Java程序要启动另一个Java程序,一般会使用ProcessBuilder来设置。 The environment variables of the new Java process will by default be the same as the current Java program, but can be modified for the new Java program by calling the environment() method of the builder.新建Java进程的环境变量默认与当前Java程序相同,但可以通过调用builder的environment()方法为新建Java程序修改。

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

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