// package java manages portable JRE downloads and detection. package java import ( "fmt" "os" "path/filepath" "runtime" "gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/config" ) // ExecutableName returns the platform-specific Java executable name. func ExecutableName() string { if runtime.GOOS == "windows" { return "java.exe" } return "java" } // IsInstalled checks whether the given Java version is available locally. func IsInstalled(version int) (string, error) { dir, err := config.JavaDir(version) if err != nil { return "", err } bin := filepath.Join(dir, "bin", ExecutableName()) if _, err := os.Stat(bin); err != nil { if os.IsNotExist(err) { return "", nil // not installed } return "", fmt.Errorf("checking %s: %w", bin, err) } return bin, nil } // Find searches for the required Java version, downloading it if necessary. // Returns the path to the java binary. func Find(version int) (string, error) { bin, err := IsInstalled(version) if err != nil { return "", err } if bin != "" { return bin, nil } // TODO: Download and extract JRE for the requested version. // For now, return an error with instructions. return "", fmt.Errorf( "Java %d is not installed. Please wait for auto-download feature "+ "(planned) or manually place JRE in %s", version, must(config.JavaDir(version)), ) } // must panics on error — only used for error messages where the dir is already known valid. func must(s string, err error) string { if err != nil { panic(err) } return s }