pub struct JavaManager { /* private fields */ }Expand description
Manages multiple Java installations and provides convenient access methods.
The JavaManager struct provides functionality to:
- Store and organize multiple Java installations
- Filter installations by various criteria
- Set default Java installations
- Execute commands with specific Java versions
§Examples
use java_manager::{JavaManager, JavaInfo};
fn main() -> java_manager::Result<()> {
// Create a manager and discover all Java installations
let mut manager = JavaManager::new();
manager.discover_installations()?;
// List all installations
println!("Found {} Java installations:", manager.len());
for java in manager.list() {
println!(" - {}", java);
}
// Get Java by version
if let Some(java_11) = manager.get_by_version(11) {
println!("Java 11: {}", java_11);
}
Ok(())
}Implementations§
Source§impl JavaManager
impl JavaManager
Sourcepub fn discover_installations(&mut self) -> Result<()>
pub fn discover_installations(&mut self) -> Result<()>
Discovers and adds all Java installations on the system.
§Returns
Ok(())if discovery succeedsErr(JavaLocatorError)if an error occurs during discovery
§Examples
use java_manager::JavaManager;
fn main() -> java_manager::Result<()> {
let mut manager = JavaManager::new();
manager.discover_installations()?;
println!("Discovered {} Java installations", manager.len());
Ok(())
}Sourcepub fn add(&mut self, java_info: JavaInfo)
pub fn add(&mut self, java_info: JavaInfo)
Adds a Java installation to the manager.
§Arguments
java_info- Java installation information to add
§Examples
use java_manager::{JavaManager, JavaInfo};
let mut manager = JavaManager::new();
let java_info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
manager.add(java_info);
assert_eq!(manager.len(), 1);Sourcepub fn get(&self, index: usize) -> Option<&JavaInfo>
pub fn get(&self, index: usize) -> Option<&JavaInfo>
Gets a Java installation by index.
§Arguments
index- Index of the Java installation to retrieve
§Returns
Some(&JavaInfo)if the index is validNoneif the index is out of bounds
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Add some installations first...
// let java = manager.get(0);Sourcepub fn get_by_version(&self, version: u32) -> Option<&JavaInfo>
pub fn get_by_version(&self, version: u32) -> Option<&JavaInfo>
Gets a Java installation by major version.
If multiple installations have the same version, returns the first one.
§Arguments
version- Major version to look for (e.g., 8, 11, 17)
§Returns
Some(&JavaInfo)if a matching installation is foundNoneif no installation with the specified version exists
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Discover installations first...
// if let Some(java_11) = manager.get_by_version(11) {
// println!("Found Java 11: {}", java_11);
// }Sourcepub fn get_all_by_version(&self, version: u32) -> Vec<&JavaInfo>
pub fn get_all_by_version(&self, version: u32) -> Vec<&JavaInfo>
Gets all Java installations of a specific major version.
§Arguments
version- Major version to look for
§Returns
Vector of references to Java installations with the specified version
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Discover installations first...
// let java_11_installations = manager.get_all_by_version(11);
// println!("Found {} Java 11 installations", java_11_installations.len());Sourcepub fn get_default(&self) -> Option<&JavaInfo>
pub fn get_default(&self) -> Option<&JavaInfo>
Gets the default Java installation.
§Returns
Some(&JavaInfo)if a default installation is setNoneif no installations exist or no default is set
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Discover installations first...
// if let Some(default_java) = manager.get_default() {
// println!("Default Java: {}", default_java);
// }Sourcepub fn set_default(&mut self, index: usize) -> bool
pub fn set_default(&mut self, index: usize) -> bool
Sets the default Java installation by index.
§Arguments
index- Index of the Java installation to set as default
§Returns
trueif the index is valid and default was setfalseif the index is out of bounds
§Examples
use java_manager::JavaManager;
let mut manager = JavaManager::new();
// Add installations first...
// let success = manager.set_default(0);Sourcepub fn set_default_by_version(&mut self, version: u32) -> bool
pub fn set_default_by_version(&mut self, version: u32) -> bool
Sets the default Java installation by version.
If multiple installations have the same version, sets the first one as default.
§Arguments
version- Major version to set as default
§Returns
trueif a matching installation was found and set as defaultfalseif no installation with the specified version exists
§Examples
use java_manager::JavaManager;
let mut manager = JavaManager::new();
// Discover installations first...
// let success = manager.set_default_by_version(11);Sourcepub fn list(&self) -> &Vec<JavaInfo>
pub fn list(&self) -> &Vec<JavaInfo>
Returns a reference to all Java installations.
§Returns
Reference to vector of all Java installations
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Discover installations first...
// let all_java = manager.list();
// for java in all_java {
// println!("{}", java);
// }Sourcepub fn filter_by_supplier(&self, supplier: &str) -> Vec<&JavaInfo>
pub fn filter_by_supplier(&self, supplier: &str) -> Vec<&JavaInfo>
Filters Java installations by supplier/vendor.
§Arguments
supplier- Supplier name to filter by (case-insensitive)
§Returns
Vector of references to Java installations from the specified supplier
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Discover installations first...
// let openjdk_installations = manager.filter_by_supplier("OpenJDK");Sourcepub fn filter_by_architecture(&self, architecture: &str) -> Vec<&JavaInfo>
pub fn filter_by_architecture(&self, architecture: &str) -> Vec<&JavaInfo>
Filters Java installations by architecture.
§Arguments
architecture- Architecture to filter by (e.g., “64-bit”, “32-bit”)
§Returns
Vector of references to Java installations with the specified architecture
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Discover installations first...
// let x64_installations = manager.filter_by_architecture("64-bit");Sourcepub fn execute_default(&self, args: &[&str]) -> Result<String>
pub fn execute_default(&self, args: &[&str]) -> Result<String>
Executes a Java command using the default Java installation.
§Arguments
args- Command-line arguments to pass to Java
§Returns
Ok(String)- Command output as a stringErr(std::io::Error)- If the command fails to execute or no default Java is set
§Examples
use java_manager::JavaManager;
fn main() -> std::io::Result<()> {
let mut manager = JavaManager::new();
manager.discover_installations()?;
let output = manager.execute_default(&["-version"])?;
println!("Output:\n{}", output);
Ok(())
}Sourcepub fn execute_with_version(
&self,
version: u32,
args: &[&str],
) -> Result<String>
pub fn execute_with_version( &self, version: u32, args: &[&str], ) -> Result<String>
Executes a Java command using a specific Java version.
§Arguments
version- Major version of Java to useargs- Command-line arguments to pass to Java
§Returns
Ok(String)- Command output as a stringErr(std::io::Error)- If the command fails to execute or the version is not found
§Examples
use java_manager::JavaManager;
fn main() -> std::io::Result<()> {
let mut manager = JavaManager::new();
manager.discover_installations()?;
let output = manager.execute_with_version(11, &["-version"])?;
println!("Java 11 output:\n{}", output);
Ok(())
}Sourcepub fn get_version_summary(&self) -> HashMap<u32, usize>
pub fn get_version_summary(&self) -> HashMap<u32, usize>
Returns a summary of Java installations by version.
§Returns
HashMap mapping major versions to counts of installations
§Examples
use java_manager::JavaManager;
let manager = JavaManager::new();
// Discover installations first...
// let summary = manager.get_version_summary();
// for (version, count) in summary {
// println!("Java {}: {} installations", version, count);
// }