Access shell environment variables in Java(在 Java 中访问 shell 环境变量)
问题描述
有谁知道如何在 Java 中访问标准 shell 环境的环境变量?我正在使用 ProcessBuilder 类,我必须指定在我正在运行的 shell 脚本中使用的特定环境变量,这些变量存在于标准 shell 环境中.
Does anyone know how to access the environment variables of the standard shell environment in Java? I am using the ProcessBuilder class and I have to specify specific environment variables used in a shell script I am running, these variables exist in the standard shell environment.
无法访问 ProcessBuilder 环境.
Accessing ProcessBuilder environment does not work.
推荐答案
您可以通过System.getenv()
获取程序创建时存在的环境变量:
You can get at the environment variables that existed when your program was created through System.getenv()
:
http://download.oracle.com/javase/tutorial/基本/环境/env.html
当 Java 应用程序使用 ProcessBuilder 对象创建新进程时,传递给新进程的默认环境变量集与提供给应用程序的虚拟机进程的环境变量集相同.应用程序可以使用 ProcessBuilder.environment 更改此设置.
When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application's virtual machine process. The application can change this set using ProcessBuilder.environment.
看起来您的子进程应该会自动获取您的环境.
It looks like your child process should get your environment automatically.
这篇关于在 Java 中访问 shell 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中访问 shell 环境变量
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01