Created
October 19, 2022 19:32
-
-
Save leo60228/66ed32f77a91918bb3d67fa912b4734e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env::{consts::EXE_SUFFIX, split_paths, var_os}; | |
use std::path::PathBuf; | |
use std::process::Command; | |
#[derive(Clone, Debug, Default)] | |
pub struct JavaExecutable { | |
pub path: PathBuf, | |
pub platform_version: Option<String>, | |
pub runtime_version: Option<String>, | |
pub jvm_information: Option<String>, | |
} | |
pub fn probe_java() -> Option<Vec<JavaExecutable>> { | |
let filename = format!("java{}", EXE_SUFFIX); | |
let paths = var_os("PATH")?; | |
Some( | |
split_paths(&paths) | |
.filter_map(|mut path| { | |
path.push(&filename); | |
if !path.exists() { | |
return None; | |
} | |
if let Ok(output) = Command::new(&path).arg("-version").output() { | |
if output.status.success() { | |
let mut lines = output | |
.stderr | |
.split(|&x| x == b'\n') | |
.map(|x| String::from_utf8_lossy(x).trim().to_string()); | |
let platform_version = lines.next(); | |
let runtime_version = lines.next(); | |
let jvm_information = lines.next(); | |
return Some(JavaExecutable { | |
path, | |
platform_version, | |
runtime_version, | |
jvm_information, | |
}); | |
} | |
} | |
Some(JavaExecutable { | |
path, | |
..Default::default() | |
}) | |
}) | |
.collect(), | |
) | |
} | |
fn main() { | |
let probed = probe_java(); | |
println!("{:#?}", probed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment