diff options
| author | 2026-01-16 07:46:13 +0100 | |
|---|---|---|
| committer | 2026-01-16 07:46:13 +0100 | |
| commit | 76ab8c504c3d094b3c9d3b2034a4c16384220926 (patch) | |
| tree | 475967b1171e7fb86f1e39f19e31b3eebe47663f /src-tauri/src/utils | |
| parent | 9fafc670ccbce9d1f457bb69d03b40988d98b961 (diff) | |
| download | DropOut-76ab8c504c3d094b3c9d3b2034a4c16384220926.tar.gz DropOut-76ab8c504c3d094b3c9d3b2034a4c16384220926.zip | |
fix(windows): resolve Java executable path issues on Windows
- Add normalize_java_path utility function with Windows-specific handling
- Automatically append .exe extension when missing on Windows
- Use 'where' command to locate java.exe in PATH if not found
- Improve error messages with full path display for debugging
- Apply path normalization in both start_game and install_forge commands
This fixes the "Failed to launch java: program not found" error on Windows
by properly handling Java executable paths, including relative paths,
missing extensions, and PATH resolution.
Reviewed-by: Claude Sonnet 4.5
Diffstat (limited to 'src-tauri/src/utils')
| -rw-r--r-- | src-tauri/src/utils/mod.rs | 1 | ||||
| -rw-r--r-- | src-tauri/src/utils/path.rs | 105 |
2 files changed, 106 insertions, 0 deletions
diff --git a/src-tauri/src/utils/mod.rs b/src-tauri/src/utils/mod.rs index 00b9087..651d26b 100644 --- a/src-tauri/src/utils/mod.rs +++ b/src-tauri/src/utils/mod.rs @@ -1,4 +1,5 @@ pub mod zip; +pub mod path; // File system related utility functions #[allow(dead_code)] diff --git a/src-tauri/src/utils/path.rs b/src-tauri/src/utils/path.rs new file mode 100644 index 0000000..453064e --- /dev/null +++ b/src-tauri/src/utils/path.rs @@ -0,0 +1,105 @@ +/// Path utilities for cross-platform compatibility +use std::path::PathBuf; + +/// Normalize a Java executable path for the current platform. +/// +/// On Windows: +/// - Adds .exe extension if missing +/// - Attempts to locate java.exe in PATH if only "java" is provided +/// - Validates that the path exists +/// +/// On Unix: +/// - Returns the path as-is +/// +/// # Arguments +/// * `java_path` - The Java executable path to normalize +/// +/// # Returns +/// * `Ok(PathBuf)` - Normalized path that exists +/// * `Err(String)` - Error if the path cannot be found or validated +#[cfg(target_os = "windows")] +pub fn normalize_java_path(java_path: &str) -> Result<PathBuf, String> { + let mut path = PathBuf::from(java_path); + + // If path doesn't exist and doesn't end with .exe, try adding .exe + if !path.exists() && path.extension().is_none() { + path.set_extension("exe"); + } + + // If still not found and it's just "java.exe", try to find it in PATH + if !path.exists() && path.file_name() == Some(std::ffi::OsStr::new("java.exe")) { + // Try to locate java.exe in PATH + if let Ok(output) = std::process::Command::new("where").arg("java").output() { + if output.status.success() { + let paths = String::from_utf8_lossy(&output.stdout); + if let Some(first_path) = paths.lines().next() { + path = PathBuf::from(first_path.trim()); + } + } + } + } + + // Verify the path exists + if !path.exists() { + return Err(format!( + "Java executable not found at: {}\nPlease configure a valid Java path in Settings.", + path.display() + )); + } + + Ok(path) +} + +#[cfg(not(target_os = "windows"))] +pub fn normalize_java_path(java_path: &str) -> Result<PathBuf, String> { + let path = PathBuf::from(java_path); + + if !path.exists() && java_path == "java" { + // Try to find java in PATH + if let Ok(output) = std::process::Command::new("which").arg("java").output() { + if output.status.success() { + let path_str = String::from_utf8_lossy(&output.stdout); + if let Some(first_path) = path_str.lines().next() { + return Ok(PathBuf::from(first_path.trim())); + } + } + } + } + + if !path.exists() && java_path != "java" { + return Err(format!( + "Java executable not found at: {}\nPlease configure a valid Java path in Settings.", + path.display() + )); + } + + Ok(path) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[cfg(target_os = "windows")] + fn test_normalize_adds_exe_extension() { + // This test assumes java is not in the current directory + let result = normalize_java_path("nonexistent_java"); + // Should fail since the file doesn't exist + assert!(result.is_err()); + } + + #[test] + fn test_normalize_existing_path() { + // Test with a path that should exist on most systems + #[cfg(target_os = "windows")] + let test_path = "C:\\Windows\\System32\\cmd.exe"; + #[cfg(not(target_os = "windows"))] + let test_path = "/bin/sh"; + + if std::path::Path::new(test_path).exists() { + let result = normalize_java_path(test_path); + assert!(result.is_ok()); + } + } +} |