Skip to main content

JavaInfo

Struct JavaInfo 

Source
pub struct JavaInfo {
    pub name: String,
    pub version: String,
    pub parsed_version: Option<JavaVersion>,
    pub path: PathBuf,
    pub vendor: String,
    pub architecture: String,
    pub java_home: PathBuf,
}
Expand description

Represents a discovered Java installation.

This struct holds metadata about a Java runtime, such as its version, vendor, architecture, and the location of its java executable and JAVA_HOME directory.

Fields§

§name: String

Human-readable name of the Java implementation (e.g., “OpenJDK”).

§version: String

Raw version string (e.g., “11.0.2”, “1.8.0_202”).

§parsed_version: Option<JavaVersion>

Parsed structured version (major/minor/patch). None if parsing failed.

§path: PathBuf

Full path to the java executable (or the path originally provided).

§vendor: String

Vendor name (e.g., “Oracle”, “OpenJDK”).

§architecture: String

Architecture (e.g., “64-Bit”, “32-Bit”).

§java_home: PathBuf

The JAVA_HOME directory corresponding to this installation.

Implementations§

Source§

impl JavaInfo

Source

pub fn execute(&self, args: &str) -> Result<(), JavaError>

Executes the Java executable with the given arguments, printing both stdout and stderr to the console.

The argument string is split using shell‑like rules (via shell_words). The child process’s stdout and stderr are captured and printed line by line while the process runs.

§Errors

Returns JavaError::IoError if spawning or waiting fails. Returns JavaError::Other if the argument string cannot be parsed. Returns JavaError::ExecutionFailed if the Java process exits with a non‑zero status.

§Examples
java.execute("-version")?;
Source

pub fn execute_with_error(&self, args: &str) -> Result<(), JavaError>

Executes the Java executable, printing only stderr to the console. Stdout is captured and discarded.

See execute for details.

§Errors

Same as execute.

Source

pub fn execute_with_output(&self, args: &str) -> Result<(), JavaError>

Executes the Java executable, printing only stdout to the console. Stderr is captured and discarded.

See execute for details.

§Errors

Same as execute.

Source§

impl JavaInfo

Source

pub fn new(path: String) -> Result<Self, JavaError>

Creates a new JavaInfo from a path pointing either to a java executable or directly to a JAVA_HOME directory.

The path is canonicalized, and if it is an executable, the JAVA_HOME is located by walking up the directory tree until a bin/java (or java.exe) is found. Metadata is then extracted from the release file inside JAVA_HOME, and any missing fields are filled by running java -version.

§Errors

Returns JavaError::InvalidJavaPath if the path does not exist, or if JAVA_HOME cannot be determined from an executable. Returns other JavaError variants if I/O or command execution fails.

§Examples
use java_manager::JavaInfo;

let info = JavaInfo::new("/usr/lib/jvm/java-11-openjdk/bin/java".into())?;
println!("Java version: {}", info.version);
Source§

impl JavaInfo

Source

pub fn matches_version(&self, req: &str) -> bool

Check whether this installation matches a version requirement.

req supports:

  • "17" — major version match (any 17.x.x)
  • "17.0" — major.minor match (any 17.0.x)
  • "17.0.2" — exact major.minor.patch match
  • "1.8" — legacy Java 8 notation (equivalent to "8")

The legacy "1." prefix (e.g. "1.8" for Java 8) is automatically stripped from the requirement before comparison, so both "1.8" and "8" match Java 8.

Returns false if the version could not be parsed.

§Examples
let info = JavaInfo {
    version: "11.0.2".into(),
    parsed_version: java_manager::JavaVersion::parse("11.0.2"),
    ..Default::default()
};

assert!( info.matches_version("11"));
assert!( info.matches_version("11.0"));
assert!( info.matches_version("11.0.2"));
assert!(!info.matches_version("17"));

let java8 = JavaInfo {
    version: "1.8.0_202".into(),
    parsed_version: java_manager::JavaVersion::parse("1.8.0_202"),
    ..Default::default()
};
assert!( java8.matches_version("1.8"));
assert!( java8.matches_version("1.8.0"));
assert!( java8.matches_version("8"));
Source

pub fn is_jdk(&self) -> bool

Returns true if this installation is a full JDK (has javac).

Checks for the presence of javac (or javac.exe on Windows) in the JAVA_HOME/bin directory. A false return typically means a JRE.

§Examples
use java_manager::java_home;

if let Some(java) = java_home() {
    if java.is_jdk() {
        println!("Full JDK detected");
    } else {
        println!("JRE only");
    }
}
Source

pub fn capabilities(&self) -> Vec<String>

Returns a list of JDK tools available in this installation.

Checks for common tools in JAVA_HOME/bin: javac, javadoc, javap, jar, jlink, jmod, jpackage, jshell, jconsole, jcmd, jmap, jstack, jstat, jhsdb, jfr, jwebserver.

Returns an empty Vec if the directory cannot be read.

§Examples
use java_manager::java_home;

if let Some(java) = java_home() {
    let tools = java.capabilities();
    println!("Available tools: {:?}", tools);
}

Trait Implementations§

Source§

impl Debug for JavaInfo

Source§

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

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

impl Default for JavaInfo

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.