Skip to main content

JavaInfo

Struct JavaInfo 

Source
pub struct JavaInfo {
    pub name: String,
    pub path: String,
    pub version: String,
    pub architecture: String,
    pub suppliers: String,
}
Expand description

Represents detailed information about a Java installation.

This struct contains all relevant information about a Java installation, including version, architecture, supplier, and path information.

§Fields

  • name: The name of the Java executable (e.g., “java”, “javac”)
  • path: Full path to the Java executable
  • version: Java version string (e.g., “11.0.12”, “1.8.0_312”)
  • architecture: Architecture information (e.g., “64-bit”, “32-bit”)
  • suppliers: Java supplier/vendor (e.g., “OpenJDK”, “Oracle”)

§Examples

use java_manager::JavaInfo;

// Create a JavaInfo instance
let java_info = JavaInfo::new(
    "java",
    "/usr/bin/java",
    "11.0.12",
    "64-bit",
    "OpenJDK"
);

println!("Java Info: {}", java_info);
println!("Major version: {:?}", java_info.get_major_version());

Fields§

§name: String

Name of the Java executable

§path: String

Full path to the Java executable

§version: String

Java version string

§architecture: String

Architecture (32-bit or 64-bit)

§suppliers: String

Java supplier/vendor

Implementations§

Source§

impl JavaInfo

Source

pub fn new( name: &str, path: &str, version: &str, architecture: &str, suppliers: &str, ) -> Self

Creates a new JavaInfo instance.

§Arguments
  • name - Name of the Java executable
  • path - Full path to the Java executable
  • version - Java version string
  • architecture - Architecture information
  • suppliers - Java supplier/vendor
§Returns

A new JavaInfo instance

§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new(
    "java",
    "/usr/bin/java",
    "11.0.12",
    "64-bit",
    "OpenJDK"
);
Source

pub fn execute(&self, args: &[&str]) -> Result<Child>

Executes a Java command asynchronously.

This method spawns a new process and returns immediately without waiting for the command to complete.

§Arguments
  • args - Command-line arguments to pass to Java
§Returns
  • Ok(Child) - A handle to the child process
  • Err(std::io::Error) - If the process cannot be spawned
§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
let child = info.execute(&["-version"]);
if let Ok(mut child) = child {
    let _ = child.wait();
}
Source

pub fn execute_and_wait(&self, args: &[&str]) -> Result<Output>

Executes a Java command and waits for completion.

This method runs the command and waits for it to complete, returning the exit status and output.

§Arguments
  • args - Command-line arguments to pass to Java
§Returns
  • Ok(Output) - Command output including status, stdout, and stderr
  • Err(std::io::Error) - If the command fails to execute
§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
let output = info.execute_and_wait(&["-version"]);
if let Ok(output) = output {
    println!("Exit status: {}", output.status);
}
Source

pub fn execute_with_output(&self, args: &[&str]) -> Result<String>

Executes a Java command and returns the output as a string.

This method captures both stdout and stderr, returning them as a string. If the command succeeds, stdout is returned. If it fails, stderr is returned.

§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
§Examples
use java_manager::JavaInfo;

fn main() -> std::io::Result<()> {
    let info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
    let output = info.execute_with_output(&["-version"])?;
    println!("Java version:\n{}", output);
    Ok(())
}
Source

pub fn execute_with_separate_output( &self, args: &[&str], ) -> Result<(String, String)>

Executes a Java command and returns both stdout and stderr as separate strings.

§Arguments
  • args - Command-line arguments to pass to Java
§Returns
  • Ok((String, String)) - Tuple containing (stdout, stderr)
  • Err(std::io::Error) - If the command fails to execute
§Examples
use java_manager::JavaInfo;

fn main() -> std::io::Result<()> {
    let info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
    let (stdout, stderr) = info.execute_with_separate_output(&["-version"])?;
    println!("Stdout:\n{}", stdout);
    println!("Stderr:\n{}", stderr);
    Ok(())
}
Source

pub fn get_major_version(&self) -> Option<u32>

Returns the major version number of Java.

Parses the version string to extract the major version. Handles both old (1.8) and new (9+) version formats.

§Returns
  • Some(u32) - Major version number
  • None - If version cannot be parsed
§Examples
use java_manager::JavaInfo;

let info1 = JavaInfo::new("java", "/usr/bin/java", "1.8.0_312", "64-bit", "OpenJDK");
assert_eq!(info1.get_major_version(), Some(8));

let info2 = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
assert_eq!(info2.get_major_version(), Some(11));
Source

pub fn is_at_least_version(&self, min_version: u32) -> bool

Checks if the Java version is at least the specified minimum version.

§Arguments
  • min_version - Minimum major version required
§Returns
  • true if Java version >= min_version
  • false if Java version < min_version or version cannot be parsed
§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
assert!(info.is_at_least_version(8));  // Java 11 >= 8
assert!(info.is_at_least_version(11)); // Java 11 >= 11
assert!(!info.is_at_least_version(17)); // Java 11 < 17
Source

pub fn get_java_home(&self) -> String

Extracts the Java home directory from the executable path.

Removes the “bin” directory from the path to get the JAVA_HOME.

§Returns

Java home directory path

§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new("java", "/usr/lib/jvm/java-11-openjdk/bin/java", "11.0.12", "64-bit", "OpenJDK");
assert_eq!(info.get_java_home(), "/usr/lib/jvm/java-11-openjdk");
Source

pub fn to_display_string(&self) -> String

Returns a display-friendly string with key Java information.

§Returns

Formatted string with Java information

§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
println!("Display: {}", info.to_display_string());
Source

pub fn is_valid(&self) -> bool

Validates that the Java executable exists and is executable.

§Returns
  • true if the executable exists and is accessible
  • false otherwise
§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new("java", "/usr/bin/java", "11.0.12", "64-bit", "OpenJDK");
if info.is_valid() {
    println!("Java installation is valid");
}

Trait Implementations§

Source§

impl Clone for JavaInfo

Source§

fn clone(&self) -> JavaInfo

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JavaInfo

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for JavaInfo

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for JavaInfo

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for JavaInfo

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.