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
impl JavaRuntime
Sourcepub fn from_executable(path: &Path) -> Result<Self, Error>
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());
Sourcepub fn new(os: &str, path: &Path, version_string: &str) -> Result<Self, Error>
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 fromenv::consts::OS
path
The path of java executable file, can be either relative or absoluteversion_string
can be like"17.0.4.1"
or the output of commandjava -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());
Sourcepub fn get_os(&self) -> &str
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.
pub fn is_windows(&self) -> bool
Sourcepub fn get_executable(&self) -> &Path
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)
Sourcepub fn has_root(&self) -> bool
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());
Sourcepub fn get_version_string(&self) -> &str
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");
Sourcepub fn is_same_os(&self) -> bool
pub fn is_same_os(&self) -> bool
Check if this is the same os as current
Sourcepub fn to_absolute(&self) -> Result<Self, Error>
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.
Sourcepub fn update(&mut self) -> Result<(), Error>
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.
Sourcepub fn is_available(&self) -> bool
pub fn is_available(&self) -> bool
Test if this runtime is available currently
It executes command java -version
to see if it works
Sourcepub fn extract_version(version_string: &str) -> Result<String, Error>
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
impl Clone for JavaRuntime
Source§fn clone(&self) -> Self
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)
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
impl Debug for JavaRuntime
Source§impl<'de> Deserialize<'de> for JavaRuntime
impl<'de> Deserialize<'de> for JavaRuntime
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for JavaRuntime
impl PartialEq for JavaRuntime
Source§fn eq(&self, other: &Self) -> bool
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);