Struct JavaRuntime

Source
pub struct JavaRuntime { /* private fields */ }
Expand description

Struct JavaRuntime Represents a java runtime in specific path.

To detect java runtimes from specific path, see detector

Implementations§

Source§

impl JavaRuntime

Source

pub fn from_executable(path: &Path) -> Result<Self, Error>

Create a JavaRuntime object from the path of java executable file

It executes command java -version to get the version information

§Parameters
  • path Path to java executable file.
§Examples
use java_runtimes::JavaRuntime;

let _ = JavaRuntime::from_executable(r"D:\java\jdk-17.0.4.1\bin\java.exe".as_ref());
let _ = JavaRuntime::from_executable(r"../../runtimes/jdk-1.8.0_291/bin/java".as_ref());
Source

pub fn new(os: &str, path: &Path, version_string: &str) -> Result<Self, Error>

Mannually create a JavaRuntime instance, without checking if it’s available

§Parameters
  • os Got from env::consts::OS
  • path The path of java executable file, can be either relative or absolute
  • version_string can be like "17.0.4.1" or the output of command java -version
§Examples
use java_runtimes::JavaRuntime;
use std::env;
use std::path::Path;

let java_exe_path = Path::new("../java/jdk-17.0.4.1/bin/java");
let version_outputs = r#"java version "17.0.4.1" 2022-08-18 LTS
Java(TM) SE Runtime Environment (build 17.0.4.1+1-LTS-2)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.4.1+1-LTS-2, mixed mode, sharing)
"#;
let runtime = JavaRuntime::new(env::consts::OS, java_exe_path, version_outputs).unwrap();
assert_eq!(runtime.get_version_string(), "17.0.4.1");
assert!(runtime.is_same_os());
Source

pub fn get_os(&self) -> &str

Get the operating system of the java runtime

The os string comes from env::consts::OS when this object was created.

Source

pub fn is_windows(&self) -> bool

Source

pub fn get_executable(&self) -> &Path

Get the path of java executable file

It can be absolute or relative, depends on how you created it.

§Examples
  • D:\Java\jdk-17.0.4.1\bin\java.exe (Windows, absolute)
  • ../../runtimes/jdk-1.8.0_291/bin/java (Linux, relative)
Source

pub fn has_root(&self) -> bool

Returns true if the Path has a root.

Refer to Path::has_root

§Examples
use java_runtimes::JavaRuntime;

let runtime = JavaRuntime::new("linux", "/jdk/bin/java".as_ref(), "21.0.3").unwrap();
assert!(runtime.has_root());

let runtime = JavaRuntime::new("windows", r"D:\jdk\bin\java.exe".as_ref(), "21.0.3").unwrap();
assert!(runtime.has_root());

let runtime = JavaRuntime::new("linux", "../jdk/bin/java".as_ref(), "21.0.3").unwrap();
assert!(!runtime.has_root());

let runtime = JavaRuntime::new("windows", r"..\jdk\bin\java.exe".as_ref(), "21.0.3").unwrap();
assert!(!runtime.has_root());
Source

pub fn get_version_string(&self) -> &str

Get the version string

§Examples
use java_runtimes::JavaRuntime;

let runtime = JavaRuntime::new("linux", "/jdk/bin/java".as_ref(), "21.0.3").unwrap();
assert_eq!(runtime.get_version_string(), "21.0.3");
Source

pub fn is_same_os(&self) -> bool

Check if this is the same os as current

Source

pub fn to_absolute(&self) -> Result<Self, Error>

Create a new JavaRuntime with absolute path.

§Errors

Returns an Err if the current working directory value is invalid. Refer to env::current_dir

Possible cases:

  • Current directory does not exist.
  • There are insufficient permissions to access the current directory.
Source

pub fn update(&mut self) -> Result<(), Error>

Try executing java -version and parse the output to get the version.

If success, it will update the version value in this JavaRuntime instance.

Source

pub fn is_available(&self) -> bool

Test if this runtime is available currently

It executes command java -version to see if it works

Source

pub fn extract_version(version_string: &str) -> Result<String, Error>

Parse version string

§Return

(version_string, version_major)

§Examples
use java_runtimes::JavaRuntime;

assert_eq!(JavaRuntime::extract_version("1.8.0_333").unwrap(), "1.8.0_333");
assert_eq!(JavaRuntime::extract_version("17.0.4.1").unwrap(), "17.0.4.1");
assert_eq!(JavaRuntime::extract_version("\"17.0.4.1").unwrap(), "17.0.4.1");
assert_eq!(JavaRuntime::extract_version("java version \"17.0.4.1\"").unwrap(), "17.0.4.1");

Trait Implementations§

Source§

impl Clone for JavaRuntime

Source§

fn clone(&self) -> Self

§Examples
use java_runtimes::JavaRuntime;

let r1 = JavaRuntime::new("linux", "/jdk/bin/java".as_ref(), "21.0.3").unwrap();
let r2 = r1.clone();

assert_eq!(r1, r2);
Source§

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

§Examples
use java_runtimes::JavaRuntime;

let mut r1 = JavaRuntime::new("windows", "/jdk/bin/java".as_ref(), "21.0.3").unwrap();
let r2 = JavaRuntime::new("windows", r"D:\jdk\bin\java.exe".as_ref(), "21.0.3").unwrap();

r1.clone_from(&r2);
assert_eq!(r1, r2);
Source§

impl Debug for JavaRuntime

Source§

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

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

impl<'de> Deserialize<'de> for JavaRuntime

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for JavaRuntime

Source§

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

§Examples
use java_runtimes::JavaRuntime;

let r1 = JavaRuntime::new("linux", "/jdk/bin/java".as_ref(), "21.0.3").unwrap();
let r2 = JavaRuntime::new("linux", "/jdk/bin/java".as_ref(), "21.0.3").unwrap();
let r3 = JavaRuntime::new("windows", r"D:\jdk\bin\java.exe".as_ref(), "21.0.3").unwrap();
let r4 = JavaRuntime::new("windows", r"D:\jdk-17\bin\java.exe".as_ref(), "21.0.3").unwrap();

assert_eq!(r1, r2);
assert_ne!(r1, r3);
assert_ne!(r2, r3);
assert_ne!(r2, r4);
assert_ne!(r3, r4);
Source§

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

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

impl Serialize for JavaRuntime

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> 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, 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,