Skip to main content

java_manager/
local.rs

1//! Functions for accessing Java from the local environment.
2
3use crate::JavaInfo;
4use std::env;
5
6/// Returns a `JavaInfo` for the Java installation pointed to by the `JAVA_HOME`
7/// environment variable, if set and valid.
8///
9/// This function reads the `JAVA_HOME` variable, treats it as a path, and attempts
10/// to create a `JavaInfo` from it. If the variable is not set or if the resulting
11/// `JavaInfo` cannot be created (e.g., the path does not contain a valid Java),
12/// `None` is returned.
13///
14/// # Examples
15///
16/// ```no_run
17/// use java_manager::java_home;
18///
19/// if let Some(java) = java_home() {
20///     println!("JAVA_HOME points to Java version {}", java.version);
21/// } else {
22///     println!("JAVA_HOME is not set or invalid");
23/// }
24/// ```
25pub fn java_home() -> Option<JavaInfo> {
26    env::var("JAVA_HOME")
27        .ok()
28        .and_then(|path| JavaInfo::new(path).ok())
29}