Skip to main content

JavaManager

Struct JavaManager 

Source
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

Source

pub fn new() -> Self

Creates a new empty JavaManager.

§Returns

A new JavaManager instance

§Examples
use java_manager::JavaManager;

let manager = JavaManager::new();
assert_eq!(manager.len(), 0);
Source

pub fn discover_installations(&mut self) -> Result<()>

Discovers and adds all Java installations on the system.

§Returns
  • Ok(()) if discovery succeeds
  • Err(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(())
}
Source

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);
Source

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 valid
  • None if the index is out of bounds
§Examples
use java_manager::JavaManager;

let manager = JavaManager::new();
// Add some installations first...
// let java = manager.get(0);
Source

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 found
  • None if 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);
// }
Source

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());
Source

pub fn get_default(&self) -> Option<&JavaInfo>

Gets the default Java installation.

§Returns
  • Some(&JavaInfo) if a default installation is set
  • None if 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);
// }
Source

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
  • true if the index is valid and default was set
  • false if 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);
Source

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
  • true if a matching installation was found and set as default
  • false if 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);
Source

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);
// }
Source

pub fn len(&self) -> usize

Returns the number of Java installations in the manager.

§Returns

Number of Java installations

§Examples
use java_manager::JavaManager;

let manager = JavaManager::new();
assert_eq!(manager.len(), 0);
Source

pub fn is_empty(&self) -> bool

Checks if the manager has any Java installations.

§Returns
  • true if there are no Java installations
  • false if there is at least one Java installation
§Examples
use java_manager::JavaManager;

let manager = JavaManager::new();
assert!(manager.is_empty());
Source

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");
Source

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");
Source

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 string
  • Err(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(())
}
Source

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 use
  • args - Command-line arguments to pass to Java
§Returns
  • Ok(String) - Command output as a string
  • Err(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(())
}
Source

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);
// }
Source

pub fn clear(&mut self)

Clears all Java installations from the manager.

§Examples
use java_manager::JavaManager;

let mut manager = JavaManager::new();
// Add some installations...
manager.clear();
assert!(manager.is_empty());

Trait Implementations§

Source§

impl Default for JavaManager

Source§

fn default() -> Self

Creates a new JavaManager with default settings.

§Returns

A new JavaManager instance

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.