fix(dameng): tolerate java 8 runtime and require java 21
This commit is contained in:
2 files changed
+338
-13
No files matched your search
@@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::agent_manager::AGENT_JAVA_TOO_OLD_MESSAGE;
|
||||
use crate::agent_recovery::{RecoveryDecision, RecoveryPolicy, RecoveryScope};
|
||||
use crate::models::connection::DatabaseConnectionInfo;
|
||||
|
||||
@@ -874,8 +875,6 @@ const AGENT_EXIT_DIAGNOSTIC_WAIT_MS: u64 = 1_000;
|
||||
const AGENT_EXIT_DIAGNOSTIC_POLL_MS: u64 = 10;
|
||||
const SHARED_RUNTIME_IDLE_GRACE_SECS: u64 = 30;
|
||||
const AGENT_JAVA_OPTS_ENV: &str = "DBX_AGENT_JAVA_OPTS";
|
||||
const AGENT_JAVA_TOO_OLD_MESSAGE: &str =
|
||||
"Agent requires Java 21, but DBX started it with an older Java runtime. Use DBX managed JRE 21 or select a Java 21 executable in Driver Manager.";
|
||||
|
||||
pub struct AgentDriverClient {
|
||||
child: Option<Child>,
|
||||
@@ -3182,7 +3181,14 @@ fn agent_java_args_with_extra_opts(
|
||||
extra_opts: Option<&str>,
|
||||
extra_java_args: &[String],
|
||||
) -> Vec<String> {
|
||||
// Java 8 and older reject the module flags below while starting up
|
||||
// ("Unrecognized option: --add-opens=..."), so the agent dies before it can
|
||||
// load a class and the real diagnosis (agent bytecode needs Java 21) is
|
||||
// replaced by a cryptic JVM error. Ignoring unrecognized VM options keeps
|
||||
// the start attempt going everywhere: pre-9 runtimes ignore the module
|
||||
// flags, Java 9+ still receives them.
|
||||
let mut args = vec![
|
||||
"-XX:+IgnoreUnrecognizedVMOptions",
|
||||
"-Dfile.encoding=UTF-8",
|
||||
"-Dsun.stdout.encoding=UTF-8",
|
||||
"-Dsun.stderr.encoding=UTF-8",
|
||||
@@ -3512,8 +3518,13 @@ fn is_agent_rpc_response_error(message: &str) -> bool {
|
||||
|
||||
fn agent_process_error_hint(stderr: &str) -> Option<&'static str> {
|
||||
let lower = stderr.to_ascii_lowercase();
|
||||
// Any class-version mismatch means the selected runtime is older than the
|
||||
// agent bytecode (Java 21 / class file 65). Match the JVM wording variants
|
||||
// independently because the stderr tail keeps only the last lines, and a
|
||||
// long stack trace can push the exception header out of the sample.
|
||||
if lower.contains("unsupportedclassversionerror")
|
||||
&& (lower.contains("class file version 65.0") || lower.contains("only recognizes class file versions up to"))
|
||||
|| lower.contains("only recognizes class file versions up to")
|
||||
|| lower.contains("class file version 65.0")
|
||||
{
|
||||
return Some(AGENT_JAVA_TOO_OLD_MESSAGE);
|
||||
}
|
||||
@@ -3568,17 +3579,17 @@ mod tests {
|
||||
use super::{
|
||||
agent_close_query_session_params, agent_error_from_legacy, agent_handshake_params, agent_java_args,
|
||||
agent_java_args_with_extra, agent_java_args_with_extra_opts, agent_object_source_params,
|
||||
agent_object_source_params_with_relation, agent_proxy_env_vars, agent_schema_params, agent_schema_table_params,
|
||||
agent_supports_capability, agent_transaction_params, append_legacy_error_context, decode_agent_response,
|
||||
format_agent_process_error, format_agent_startup_error, is_agent_rpc_response_error,
|
||||
agent_object_source_params_with_relation, agent_process_error_hint, agent_proxy_env_vars, agent_schema_params,
|
||||
agent_schema_table_params, agent_supports_capability, agent_transaction_params, append_legacy_error_context,
|
||||
decode_agent_response, format_agent_process_error, format_agent_startup_error, is_agent_rpc_response_error,
|
||||
is_unsupported_handshake_error, legacy_agent_call_error, mongo_collection_params, mongo_database_params,
|
||||
mongo_document_id_params, parse_agent_java_opts, read_agent_json_response, read_agent_line,
|
||||
read_agent_line_with_limit, start_stderr_collector, validate_dameng_java_system_properties, AgentCallError,
|
||||
AgentCapability, AgentDriverClient, AgentErrorCategory, AgentErrorContext, AgentErrorStage, AgentHandshake,
|
||||
AgentKvMethod, AgentLaunchSpec, AgentMethod, AgentOperationOutcome, AgentRuntimeClient,
|
||||
AgentSessionDisposition, AgentTableReadCloseParams, AgentTableReadPageParams, AgentTableReadStartParams,
|
||||
MongoAgentMethod, StderrTail, AGENT_PROTOCOL_VERSION, AGENT_STDOUT_NOISE_SAMPLE_CHARS,
|
||||
MAX_CONSECUTIVE_AGENT_STDOUT_NOISE_LINES,
|
||||
MongoAgentMethod, StderrTail, AGENT_JAVA_TOO_OLD_MESSAGE, AGENT_PROTOCOL_VERSION,
|
||||
AGENT_STDOUT_NOISE_SAMPLE_CHARS, MAX_CONSECUTIVE_AGENT_STDOUT_NOISE_LINES,
|
||||
};
|
||||
use crate::agent_recovery::{RecoveryDecision, RecoveryPolicy, RecoveryScope};
|
||||
use std::io::Cursor;
|
||||
@@ -4022,6 +4033,60 @@ mod tests {
|
||||
assert!(!args.iter().any(|arg| arg == "--add-opens=java.base/java.net=ALL-UNNAMED"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_java_args_tolerate_runtimes_without_module_support() {
|
||||
// Regression: a Java 8 runtime rejects "--add-opens=..." while starting
|
||||
// up, so connecting with a driver that adds module flags failed with
|
||||
// "Unrecognized option: --add-opens=..." instead of the actionable
|
||||
// "this driver needs Java 21" message.
|
||||
for jar in [
|
||||
"/tmp/dbx/drivers/dameng/agent.jar",
|
||||
"/tmp/dbx/drivers/ignite/agent.jar",
|
||||
"/tmp/dbx/drivers/highgo/agent.jar",
|
||||
] {
|
||||
let args = agent_java_args(jar);
|
||||
let ignore_unrecognized = args
|
||||
.iter()
|
||||
.position(|arg| arg == "-XX:+IgnoreUnrecognizedVMOptions")
|
||||
.unwrap_or_else(|| panic!("{jar} should tolerate runtimes without module support"));
|
||||
let first_module_flag = args
|
||||
.iter()
|
||||
.position(|arg| arg.starts_with("--add-opens="))
|
||||
.unwrap_or_else(|| panic!("{jar} should still open the JDK modules it needs"));
|
||||
|
||||
assert!(
|
||||
ignore_unrecognized < first_module_flag,
|
||||
"module flags must be preceded by the ignore flag: {args:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_process_error_hint_matches_java_8_startup_failure() {
|
||||
// Captured from a real Java 8 runtime that was handed the module flags.
|
||||
let module_rejection = "Unrecognized option: --add-opens=java.base/java.net=ALL-UNNAMED\n\
|
||||
Error: Could not create the Java Virtual Machine.\n\
|
||||
Error: A fatal exception has occurred. Program will exit.";
|
||||
assert_eq!(agent_process_error_hint(module_rejection), None);
|
||||
|
||||
let full_class_version = "Error: A JNI error has occurred, please check your installation and try again\n\
|
||||
Exception in thread \"main\" java.lang.UnsupportedClassVersionError: com/dbx/agent/dameng/DamengAgent has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 52.0\n\
|
||||
\tat sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:634)";
|
||||
assert_eq!(agent_process_error_hint(full_class_version), Some(AGENT_JAVA_TOO_OLD_MESSAGE));
|
||||
|
||||
// Only the last stderr lines survive in the tail, so anything from the
|
||||
// class-version failure is enough on its own.
|
||||
assert_eq!(
|
||||
agent_process_error_hint("java.lang.UnsupportedClassVersionError"),
|
||||
Some(AGENT_JAVA_TOO_OLD_MESSAGE)
|
||||
);
|
||||
assert_eq!(
|
||||
agent_process_error_hint("this version of the Java Runtime only recognizes class file versions up to 52.0"),
|
||||
Some(AGENT_JAVA_TOO_OLD_MESSAGE)
|
||||
);
|
||||
assert_eq!(agent_process_error_hint("java.net.ConnectException: Connection refused"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_java_args_open_jdk_internals_for_ignite() {
|
||||
let args = agent_java_args("/tmp/dbx/drivers/ignite/agent.jar");
|
||||
|
||||
@@ -3,7 +3,9 @@ use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::ops::Deref;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
use std::sync::{Arc, Mutex as StdMutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::sync::Mutex;
|
||||
@@ -14,9 +16,20 @@ use crate::db::agent_driver::{
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub const DEFAULT_JRE_KEY: &str = "21";
|
||||
/// Every Java artifact DBX runs itself (driver agents and the JDBC plugin) is
|
||||
/// compiled for Java 21, so an older runtime cannot load any of them.
|
||||
pub const MINIMUM_JAVA_MAJOR_VERSION: u32 = 21;
|
||||
/// Shown when the selected Java runtime is older than the agent bytecode.
|
||||
/// Kept verbatim in sync with the desktop error catalog
|
||||
/// (apps/desktop/src/i18n/backend-errors.ts -> connection.agentJavaTooOld).
|
||||
pub const AGENT_JAVA_TOO_OLD_MESSAGE: &str =
|
||||
"Agent requires Java 21, but DBX started it with an older Java runtime. Use DBX managed JRE 21 or select a Java 21 executable in Driver Manager.";
|
||||
pub const SQLITE_WORKER_DRIVER_KEY: &str = "sqlite-worker";
|
||||
pub const SQLITE_WORKER_NATIVE_PLATFORMS: &[&str] = &["linux-x64", "linux-aarch64"];
|
||||
pub const DOWNLOAD_CACHE_DIR_NAME: &str = "download-cache";
|
||||
/// `java -version` is only needed for runtimes without a `release` file; the
|
||||
/// probe must never block a connect or a settings save on a broken executable.
|
||||
pub const JAVA_VERSION_PROBE_TIMEOUT_SECS: u64 = 5;
|
||||
pub const DOWNLOAD_CACHE_MAX_AGE_DAYS: u64 = 7;
|
||||
|
||||
pub type OperationLockTable = StdMutex<std::collections::HashMap<String, Arc<Mutex<()>>>>;
|
||||
@@ -254,6 +267,126 @@ mod tests {
|
||||
assert_eq!(manager.resolve_java_runtime(&state, DEFAULT_JRE_KEY).unwrap(), java);
|
||||
}
|
||||
|
||||
fn temp_dir(name: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-agent-manager-{name}-{}", uuid::Uuid::new_v4()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_java_major_versions() {
|
||||
assert_eq!(parse_java_major_version("1.8.0_504"), Some(8));
|
||||
assert_eq!(parse_java_major_version("1.7.0_80"), Some(7));
|
||||
assert_eq!(parse_java_major_version("21.0.10"), Some(21));
|
||||
assert_eq!(parse_java_major_version("17.0.18+9"), Some(17));
|
||||
assert_eq!(parse_java_major_version("\"11.0.2\""), Some(11));
|
||||
assert_eq!(parse_java_major_version("9"), Some(9));
|
||||
assert_eq!(parse_java_major_version("not-a-version"), None);
|
||||
assert_eq!(parse_java_major_version(""), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_java_version_output() {
|
||||
assert_eq!(
|
||||
parse_java_version_output(
|
||||
"openjdk version \"1.8.0_504\"\nOpenJDK Runtime Environment (build 1.8.0_504-b01)"
|
||||
)
|
||||
.as_deref(),
|
||||
Some("1.8.0_504")
|
||||
);
|
||||
assert_eq!(parse_java_version_output("java version \"21.0.10\" 2026-01-20 LTS").as_deref(), Some("21.0.10"));
|
||||
assert_eq!(parse_java_version_output("command not found"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_java_version_from_release_file_in_both_layouts() {
|
||||
let home = temp_dir("release-flat");
|
||||
let flat_java = home.join("bin").join(java_executable_name());
|
||||
touch(&flat_java);
|
||||
fs::write(home.join("release"), "IMPLEMENTOR=\"Eclipse Adoptium\"\nJAVA_VERSION=\"1.8.0_504\"\n").unwrap();
|
||||
|
||||
assert_eq!(AgentManager::java_major_version(&flat_java), Some(8));
|
||||
|
||||
let bundle = temp_dir("release-bundle");
|
||||
let bundle_java = bundle.join("Contents").join("Home").join("bin").join(java_executable_name());
|
||||
touch(&bundle_java);
|
||||
fs::write(bundle.join("Contents").join("Home").join("release"), "JAVA_VERSION=\"21.0.10\"\n").unwrap();
|
||||
|
||||
assert_eq!(AgentManager::java_major_version(&bundle_java), Some(21));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn probes_java_version_when_release_file_is_missing() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let home = temp_dir("probe-version");
|
||||
let java = home.join("bin").join("java");
|
||||
fs::create_dir_all(java.parent().unwrap()).unwrap();
|
||||
fs::write(&java, "#!/bin/sh\necho 'openjdk version \"1.8.0_504\"' >&2\n").unwrap();
|
||||
fs::set_permissions(&java, fs::Permissions::from_mode(0o755)).unwrap();
|
||||
|
||||
assert_eq!(AgentManager::java_major_version(&java), Some(8));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_java_runtime_older_than_the_agent_bytecode() {
|
||||
let manager = test_manager("old-custom-java");
|
||||
let java = manager.base_dir().join("jre8").join("bin").join(java_executable_name());
|
||||
touch(&java);
|
||||
// The reported case: a Java 8 runtime (a system Java or an imported JRE)
|
||||
// rejects the agent's --add-opens flags before any class is loaded.
|
||||
fs::write(java.parent().unwrap().parent().unwrap().join("release"), "JAVA_VERSION=\"1.8.0_504\"\n").unwrap();
|
||||
let state = AgentState {
|
||||
java_runtime: JavaRuntimeConfig {
|
||||
mode: JavaRuntimeMode::Custom,
|
||||
custom_java_path: Some(java.to_string_lossy().to_string()),
|
||||
},
|
||||
..AgentState::default()
|
||||
};
|
||||
|
||||
let err = manager.resolve_java_runtime(&state, DEFAULT_JRE_KEY).unwrap_err();
|
||||
|
||||
assert!(err.starts_with(AGENT_JAVA_TOO_OLD_MESSAGE), "unexpected error: {err}");
|
||||
assert!(err.contains("Detected Java 8"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_java_runtime_matching_the_agent_bytecode() {
|
||||
let manager = test_manager("new-custom-java");
|
||||
let java = manager.base_dir().join("jre21").join("bin").join(java_executable_name());
|
||||
touch(&java);
|
||||
fs::write(java.parent().unwrap().parent().unwrap().join("release"), "JAVA_VERSION=\"21.0.10\"\n").unwrap();
|
||||
let state = AgentState {
|
||||
java_runtime: JavaRuntimeConfig {
|
||||
mode: JavaRuntimeMode::Custom,
|
||||
custom_java_path: Some(java.to_string_lossy().to_string()),
|
||||
},
|
||||
..AgentState::default()
|
||||
};
|
||||
|
||||
assert_eq!(manager.resolve_java_runtime(&state, DEFAULT_JRE_KEY).unwrap(), java);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_runtimes_with_unknown_version_usable() {
|
||||
// Wrapper scripts and stripped runtimes cannot be probed; they must still
|
||||
// resolve so that the spawn-time diagnostics stay in charge.
|
||||
let manager = test_manager("unknown-custom-java");
|
||||
let java = manager.base_dir().join("wrapper").join("bin").join(java_executable_name());
|
||||
touch(&java);
|
||||
let state = AgentState {
|
||||
java_runtime: JavaRuntimeConfig {
|
||||
mode: JavaRuntimeMode::Custom,
|
||||
custom_java_path: Some(java.to_string_lossy().to_string()),
|
||||
},
|
||||
..AgentState::default()
|
||||
};
|
||||
|
||||
assert_eq!(AgentManager::java_major_version(&java), None);
|
||||
assert_eq!(manager.resolve_java_runtime(&state, DEFAULT_JRE_KEY).unwrap(), java);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_custom_java_runtime_when_configured() {
|
||||
let manager = test_manager("custom");
|
||||
@@ -1135,21 +1268,39 @@ impl AgentManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Major version of the Java runtime at `java_path`, or `None` when it
|
||||
/// cannot be determined (wrapper scripts, stripped runtimes, unknown
|
||||
/// layouts). An undetectable runtime is never rejected: the spawn-time
|
||||
/// diagnostics in `agent_driver` still explain a failed start.
|
||||
pub fn java_major_version(java_path: &Path) -> Option<u32> {
|
||||
let key = java_path.canonicalize().unwrap_or_else(|_| java_path.to_path_buf());
|
||||
if let Ok(cache) = java_version_cache().lock() {
|
||||
if let Some(cached) = cache.get(&key) {
|
||||
return *cached;
|
||||
}
|
||||
}
|
||||
let detected = detect_java_major_version(&key);
|
||||
if let Ok(mut cache) = java_version_cache().lock() {
|
||||
cache.insert(key, detected);
|
||||
}
|
||||
detected
|
||||
}
|
||||
|
||||
pub fn resolve_java_runtime(&self, state: &AgentState, jre_key: &str) -> Result<PathBuf, String> {
|
||||
match state.java_runtime.mode {
|
||||
let java = match state.java_runtime.mode {
|
||||
JavaRuntimeMode::Managed => {
|
||||
if !self.is_jre_installed(jre_key) {
|
||||
return Err(format!(
|
||||
"JRE {jre_key} runtime is not installed. Please install it from the Driver Manager."
|
||||
));
|
||||
}
|
||||
Ok(self.jre_java_path(jre_key))
|
||||
self.jre_java_path(jre_key)
|
||||
}
|
||||
JavaRuntimeMode::System => {
|
||||
let java_home = std::env::var_os(JAVA_HOME_ENV);
|
||||
let path_var = std::env::var_os(PATH_ENV);
|
||||
resolve_system_java_path(java_home.as_deref(), path_var.as_deref())
|
||||
.ok_or_else(|| system_java_missing_error(java_home.as_deref(), path_var.as_deref()))
|
||||
.ok_or_else(|| system_java_missing_error(java_home.as_deref(), path_var.as_deref()))?
|
||||
}
|
||||
JavaRuntimeMode::Custom => {
|
||||
let path = state
|
||||
@@ -1159,9 +1310,11 @@ impl AgentManager {
|
||||
.map(str::trim)
|
||||
.filter(|path| !path.is_empty())
|
||||
.ok_or_else(|| "Custom Java runtime path is empty. Please choose a Java executable.".to_string())?;
|
||||
resolve_custom_java_path(path)
|
||||
resolve_custom_java_path(path)?
|
||||
}
|
||||
}
|
||||
};
|
||||
ensure_supported_java_runtime(&java)?;
|
||||
Ok(java)
|
||||
}
|
||||
|
||||
pub async fn stop_daemons(&self) {
|
||||
@@ -1370,6 +1523,113 @@ fn resolve_custom_java_path(path: &str) -> Result<PathBuf, String> {
|
||||
.ok_or_else(|| format!("Custom Java runtime does not exist or is not a Java executable: {}", raw.display()))
|
||||
}
|
||||
|
||||
/// Rejects a runtime that cannot load DBX's Java artifacts before any agent
|
||||
/// process is spawned, so the user gets actionable guidance instead of a raw
|
||||
/// JVM startup failure (a Java 8 runtime dies on the `--add-opens` flags with
|
||||
/// "Unrecognized option: --add-opens=...").
|
||||
fn ensure_supported_java_runtime(java_path: &Path) -> Result<(), String> {
|
||||
let Some(major) = AgentManager::java_major_version(java_path) else {
|
||||
return Ok(());
|
||||
};
|
||||
if major >= MINIMUM_JAVA_MAJOR_VERSION {
|
||||
return Ok(());
|
||||
}
|
||||
Err(format!("{AGENT_JAVA_TOO_OLD_MESSAGE} Detected Java {major} at {}.", java_path.display()))
|
||||
}
|
||||
|
||||
fn java_version_cache() -> &'static StdMutex<std::collections::HashMap<PathBuf, Option<u32>>> {
|
||||
static CACHE: std::sync::OnceLock<StdMutex<std::collections::HashMap<PathBuf, Option<u32>>>> =
|
||||
std::sync::OnceLock::new();
|
||||
CACHE.get_or_init(Default::default)
|
||||
}
|
||||
|
||||
fn detect_java_major_version(java_path: &Path) -> Option<u32> {
|
||||
if let Some(major) = read_java_release_version(java_path).and_then(|version| parse_java_major_version(&version)) {
|
||||
return Some(major);
|
||||
}
|
||||
let output = probe_java_version_output(java_path)?;
|
||||
let version = parse_java_version_output(&output)?;
|
||||
parse_java_major_version(&version)
|
||||
}
|
||||
|
||||
/// Reads `JAVA_VERSION` from the `release` file of the Java home that contains
|
||||
/// `java_path`, which avoids starting a JVM just to learn the version. Walks a
|
||||
/// few levels up to cover both the `<home>/bin/java` and the macOS bundle
|
||||
/// `<home>/Contents/Home/bin/java` layouts.
|
||||
fn read_java_release_version(java_path: &Path) -> Option<String> {
|
||||
let bin_dir = java_path.parent()?;
|
||||
for ancestor in bin_dir.ancestors().take(4) {
|
||||
let Ok(contents) = std::fs::read_to_string(ancestor.join("release")) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(version) = parse_release_java_version(&contents) {
|
||||
return Some(version);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn parse_release_java_version(contents: &str) -> Option<String> {
|
||||
contents
|
||||
.lines()
|
||||
.find_map(|line| line.strip_prefix("JAVA_VERSION="))
|
||||
.map(|value| value.trim().trim_matches('"').to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
}
|
||||
|
||||
/// Extracts the version from `java -version` output, e.g.
|
||||
/// `openjdk version "1.8.0_504"` or `java version "21.0.10" 2024-01-16 LTS`.
|
||||
fn parse_java_version_output(output: &str) -> Option<String> {
|
||||
output.lines().find_map(|line| {
|
||||
let (_, rest) = line.split_once("version ")?;
|
||||
let quoted = rest.trim_start().strip_prefix('"')?;
|
||||
quoted.split('"').next().filter(|value| !value.is_empty()).map(str::to_string)
|
||||
})
|
||||
}
|
||||
|
||||
/// `1.8.0_504` -> 8, `21.0.10` -> 21, `9` -> 9.
|
||||
fn parse_java_major_version(version: &str) -> Option<u32> {
|
||||
let cleaned = version.trim().trim_matches('"');
|
||||
let mut parts = cleaned.split(['.', '_', '-', '+']);
|
||||
let first = parts.next()?.parse::<u32>().ok()?;
|
||||
if first == 1 {
|
||||
parts.next()?.parse::<u32>().ok()
|
||||
} else {
|
||||
Some(first)
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `java -version` (which writes to stderr) for runtimes that ship no
|
||||
/// `release` file. Bounded by a timeout so a broken executable can never hang a
|
||||
/// connect or a settings save.
|
||||
fn probe_java_version_output(java_path: &Path) -> Option<String> {
|
||||
let mut child = crate::process::new_std_command(java_path)
|
||||
.arg("-version")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.ok()?;
|
||||
let deadline = Instant::now() + Duration::from_secs(JAVA_VERSION_PROBE_TIMEOUT_SECS);
|
||||
loop {
|
||||
match child.try_wait() {
|
||||
Ok(Some(_)) => break,
|
||||
Ok(None) if Instant::now() < deadline => std::thread::sleep(Duration::from_millis(10)),
|
||||
_ => {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
let output = child.wait_with_output().ok()?;
|
||||
let mut text = String::from_utf8_lossy(&output.stderr).into_owned();
|
||||
if text.trim().is_empty() {
|
||||
text = String::from_utf8_lossy(&output.stdout).into_owned();
|
||||
}
|
||||
Some(text)
|
||||
}
|
||||
|
||||
const JAVA_HOME_ENV: &str = "JAVA_HOME";
|
||||
const PATH_ENV: &str = "PATH";
|
||||
|
||||
|
||||
Reference in new issue
Block a user