Skip to content

Commit aa0a801

Browse files
authored
fix(cli): resolve java symlinks and macOS stub when detecting JAVA_HOME (#16116)
1 parent 4f51a2f commit aa0a801

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Fix Java version detection for the Android Java/Gradle compatibility warning when `JAVA_HOME` is not set: the `java` binary on `PATH` is now resolved through symlinks (e.g. Linux alternatives and Homebrew), and on macOS the `/usr/bin/java` stub falls back to `/usr/libexec/java_home`.

‎crates/tauri-cli/src/mobile/android/check_java_gradle_versions.rs‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,36 @@ fn java_major_version() -> Option<u32> {
8383

8484
/// Resolves the active JDK home directory, preferring `JAVA_HOME` and otherwise
8585
/// deriving it from the `java` binary on `PATH` (`<home>/bin/java` -> `<home>`).
86+
/// Symlinks are resolved first so shims such as Linux alternatives
87+
/// (`/usr/bin/java` -> `/usr/lib/jvm/<jdk>/bin/java`) point at the actual JDK.
8688
fn java_home_dir() -> Option<PathBuf> {
8789
if let Some(home) = std::env::var_os("JAVA_HOME") {
8890
return Some(PathBuf::from(home));
8991
}
90-
let java = which::which("java").ok()?;
91-
java.parent()?.parent().map(Path::to_path_buf)
92+
let java = dunce::canonicalize(which::which("java").ok()?).ok()?;
93+
let home = java.parent()?.parent().map(Path::to_path_buf)?;
94+
95+
// on macOS `/usr/bin/java` is a stub binary that launches the JDK selected by `java_home`
96+
#[cfg(target_os = "macos")]
97+
if !home.join("release").exists() {
98+
return macos_java_home();
99+
}
100+
101+
Some(home)
102+
}
103+
104+
/// Asks `/usr/libexec/java_home` for the default JDK home directory.
105+
#[cfg(target_os = "macos")]
106+
fn macos_java_home() -> Option<PathBuf> {
107+
let output = std::process::Command::new("/usr/libexec/java_home")
108+
.output()
109+
.ok()?;
110+
if !output.status.success() {
111+
return None;
112+
}
113+
let home = String::from_utf8(output.stdout).ok()?;
114+
let home = home.trim();
115+
(!home.is_empty()).then(|| PathBuf::from(home))
92116
}
93117

94118
/// Parses the major (feature) version out of a quoted Java version string (the

0 commit comments

Comments
 (0)