Class Environment
Provides cached access to JDK, OS, and runtime information.
Static values (JDK version, OS name, container detection) are cached via Lazy
for thread-safe single evaluation. Resource metrics (memory, processors) are
returned live on each call.
提供对 JDK、操作系统和运行时信息的缓存访问。
静态值(JDK 版本、操作系统名称、容器检测)通过 Lazy 缓存,
保证线程安全的单次求值。资源指标(内存、处理器)每次调用返回实时值。
Features | 主要功能:
- JDK version and vendor detection - JDK 版本和厂商检测
- OS type detection (Windows, Linux, macOS) - 操作系统类型检测
- GraalVM native-image detection - GraalVM 原生镜像检测
- Container environment detection (Docker, Kubernetes, cgroup) - 容器环境检测
- Virtual thread detection - 虚拟线程检测
- Live runtime resource metrics - 实时运行时资源指标
- Process info (pid, uptime, javaHome, userDir, tempDir) - 进程信息
Usage Examples | 使用示例:
// Check JDK version
if (Environment.isJavaVersionAtLeast(21)) {
// Use virtual threads
}
// OS-specific logic
if (Environment.isWindows()) {
// Windows-specific path handling
}
// Container-aware resource allocation
if (Environment.isContainer()) {
int cpus = Environment.availableProcessors();
// Adjust thread pool size for container limits
}
// Virtual thread detection
if (Environment.isVirtualThread()) {
// Avoid thread-local caching
}
Thread Safety | 线程安全:
- Since:
- JDK 25, opencode-base-core V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intReturns the number of processors available to the JVM.static longReturns the amount of free memory in the JVM, in bytes.static booleanBest-effort detection of whether the JVM is running inside a container (Docker, Kubernetes, etc.).static booleanChecks whether the code is running as a GraalVM native image.static booleanisJavaVersionAtLeast(int version) Checks whether the current Java version is at least the given version.static booleanisLinux()Checks whether the current OS is Linux.static booleanisMacOS()Checks whether the current OS is macOS (or Darwin).static booleanChecks whether the current thread is a virtual thread.static booleanChecks whether the current OS is Windows.static StringjavaHome()Returns the Java home directory path.static StringReturns the Java vendor string.static intReturns the Java feature version number (e.g. 25 for JDK 25).static longReturns the maximum amount of memory the JVM will attempt to use, in bytes.static StringosName()Returns the operating system name.static longpid()Returns the process ID (PID) of the current JVM.static StringtempDir()Returns the system temporary directory path.static longReturns the total amount of memory currently available to the JVM, in bytes.static Durationuptime()Returns the uptime of the JVM since it started.static StringuserDir()Returns the user's current working directory.
-
Method Details
-
javaVersion
public static int javaVersion()Returns the Java feature version number (e.g. 25 for JDK 25). 返回 Java 特性版本号(例如 JDK 25 返回 25)。- Returns:
- the Java feature version
-
javaVendor
Returns the Java vendor string. 返回 Java 厂商字符串。- Returns:
- the Java vendor, or
nullif the property is not set
-
isJavaVersionAtLeast
public static boolean isJavaVersionAtLeast(int version) Checks whether the current Java version is at least the given version. 检查当前 Java 版本是否至少为给定版本。- Parameters:
version- the minimum version to check against - 要检查的最低版本- Returns:
trueif the current Java version is >= the given version
-
osName
Returns the operating system name. 返回操作系统名称。- Returns:
- the OS name from
os.namesystem property, or empty string if not set
-
isWindows
public static boolean isWindows()Checks whether the current OS is Windows. 检查当前操作系统是否为 Windows。- Returns:
trueif running on Windows
-
isLinux
public static boolean isLinux()Checks whether the current OS is Linux. 检查当前操作系统是否为 Linux。- Returns:
trueif running on Linux
-
isMacOS
public static boolean isMacOS()Checks whether the current OS is macOS (or Darwin). 检查当前操作系统是否为 macOS(或 Darwin)。- Returns:
trueif running on macOS
-
isGraalVmNativeImage
public static boolean isGraalVmNativeImage()Checks whether the code is running as a GraalVM native image. 检查代码是否以 GraalVM 原生镜像方式运行。Detection is based on the
org.graalvm.nativeimage.imagecodesystem property.基于
org.graalvm.nativeimage.imagecode系统属性进行检测。- Returns:
trueif running in a GraalVM native image
-
isContainer
public static boolean isContainer()Best-effort detection of whether the JVM is running inside a container (Docker, Kubernetes, etc.). 尽力检测 JVM 是否在容器(Docker、Kubernetes 等)中运行。Warning: This is best-effort detection and MUST NOT be used for security decisions. The detection checks the following indicators:
警告:这是尽力检测,不得用于安全决策。检测检查以下指标:
/.dockerenvfile existence - 文件是否存在KUBERNETES_SERVICE_HOSTenvironment variable - 环境变量/proc/1/cgroupfor container indicators - 容器指标/proc/1/mountinfofor container mount points - 容器挂载点
- Returns:
trueif container indicators are detected (best-effort, NOT for security decisions)
-
isVirtualThread
public static boolean isVirtualThread()Checks whether the current thread is a virtual thread. 检查当前线程是否为虚拟线程。This method is NOT cached because it depends on the calling thread.
此方法不缓存,因为结果取决于调用线程。
- Returns:
trueif the current thread is a virtual thread
-
availableProcessors
public static int availableProcessors()Returns the number of processors available to the JVM. 返回 JVM 可用的处理器数量。This value may change during the lifetime of the JVM, especially in containerized environments. The value is NOT cached.
此值在 JVM 生命周期内可能会变化,尤其是在容器化环境中。该值不缓存。
- Returns:
- the number of available processors
-
maxMemory
public static long maxMemory()Returns the maximum amount of memory the JVM will attempt to use, in bytes. 返回 JVM 将尝试使用的最大内存量(字节)。- Returns:
- the maximum memory in bytes
-
totalMemory
public static long totalMemory()Returns the total amount of memory currently available to the JVM, in bytes. 返回当前 JVM 可用的总内存量(字节)。- Returns:
- the total memory in bytes
-
freeMemory
public static long freeMemory()Returns the amount of free memory in the JVM, in bytes. 返回 JVM 中的空闲内存量(字节)。- Returns:
- the free memory in bytes
-
pid
public static long pid()Returns the process ID (PID) of the current JVM. 返回当前 JVM 的进程 ID(PID)。- Returns:
- the process ID | 进程 ID
-
uptime
Returns the uptime of the JVM since it started. 返回 JVM 自启动以来的运行时间。- Returns:
- the JVM uptime duration | JVM 运行时间
-
javaHome
Returns the Java home directory path. 返回 Java 安装目录路径。- Returns:
- the Java home path | Java 安装路径
-
userDir
Returns the user's current working directory. 返回用户当前工作目录。- Returns:
- the current working directory | 当前工作目录
-
tempDir
Returns the system temporary directory path. 返回系统临时目录路径。- Returns:
- the temp directory path | 临时目录路径
-