java_manager/lib.rs
1//! A library for locating Java installations on the local system and executing Java programs.
2//!
3//! This crate provides functionality to:
4//! - Discover Java runtimes via `PATH`, `JAVA_HOME`, or deep system scans.
5//! - Extract detailed metadata (version, vendor, architecture) from each installation.
6//! - Execute Java applications with configurable arguments, memory settings, and I/O redirection.
7//!
8//! # Examples
9//!
10//! ```no_run
11//! use java_manager::{java_home, JavaRunner};
12//!
13//! // Find all Java installations in PATH
14//! let java = java_home().unwrap();
15//! // Run a JAR file
16//! JavaRunner::new()
17//! .java(java)
18//! .arg("--version")
19//! .execute()?;
20//! # Ok::<_, java_manager::JavaError>(())
21//! ```
22
23/// TTL-based cache that avoids redundant full-disk scans of Java installations.
24pub mod cache;
25
26/// Error types returned by every fallible operation in this crate.
27pub mod error;
28
29/// Execute Java programs with configurable arguments, memory, and I/O.
30pub mod execute;
31
32/// [`JavaInfo`] and [`JavaVersion`] — structured metadata from a Java installation.
33pub mod info;
34
35/// Read the `JAVA_HOME` environment variable.
36pub mod local;
37
38/// Discover Java installations via `PATH`, Everything SDK, registry, BFS, and more.
39pub mod search;
40
41/// Async JDK download with resume support, parallel chunks, and archive extraction.
42#[cfg(feature = "download")]
43pub mod download;
44
45pub use cache::JavaCache;
46pub use error::JavaError;
47pub use execute::JavaRedirect;
48pub use execute::JavaRunner;
49pub use info::JavaInfo;
50pub use info::JavaVersion;
51pub use local::java_home;
52pub use search::deep_search;
53pub use search::full_search;
54pub use search::quick_search;
55
56/// Human-readable Java spec parsing and matching (`"Eclipse Adoptium v^17.0.0 ax86_64"`).
57pub mod spec;
58
59#[cfg(feature = "parallel")]
60pub use search::parallel_full_search;
61pub use spec::JavaSpec;
62
63/// Filter a list of `JavaInfo` by a version requirement.
64///
65/// See [`JavaInfo::matches_version`] for the supported requirement formats.
66pub fn filter_by_version(javas: Vec<JavaInfo>, req: &str) -> Vec<JavaInfo> {
67 javas
68 .into_iter()
69 .filter(|j| j.matches_version(req))
70 .collect()
71}
72
73/// Pick the best (highest version) match from a list of `JavaInfo`.
74///
75/// Returns `None` if no installation matches the requirement.
76///
77/// # Examples
78///
79/// ```
80/// use java_manager::{JavaInfo, best_match};
81///
82/// let javas = vec![
83/// JavaInfo { version: "11.0.2".into(), parsed_version: java_manager::JavaVersion::parse("11.0.2"), ..Default::default() },
84/// JavaInfo { version: "17.0.1".into(), parsed_version: java_manager::JavaVersion::parse("17.0.1"), ..Default::default() },
85/// ];
86///
87/// let best = best_match(javas, "11").unwrap();
88/// assert_eq!(best.version, "11.0.2");
89/// ```
90pub fn best_match(javas: Vec<JavaInfo>, req: &str) -> Option<JavaInfo> {
91 javas
92 .into_iter()
93 .filter(|j| j.matches_version(req))
94 .max_by(|a, b| a.parsed_version.cmp(&b.parsed_version))
95}