Created
August 25, 2015 19:07
-
-
Save leikahing/4ec9969520ed1a18753f to your computer and use it in GitHub Desktop.
Finding Java in the registry using F#
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
let rec findWorkingJava registryKeys = | |
match registryKeys with | |
| [] -> "" | |
| x::xs -> | |
let value = Registry.GetValue(x, "JavaHome", String.Empty) | |
match value with | |
| null -> findWorkingJava xs | |
| _ -> value.ToString() | |
let findJavaInRegistry registryPath = | |
let subKeyPath = sprintf @"Software\JavaSoft\%s" registryPath | |
use javaRegKey = Registry.LocalMachine.OpenSubKey(subKeyPath) | |
let javaSubKeys = | |
match javaRegKey with | |
| null -> [] | |
| key -> | |
key.GetSubKeyNames() |> Array.map (fun x -> (sprintf @"%s\%s" (javaRegKey.ToString()) (x.ToString()))) |> Array.toList | |
findWorkingJava javaSubKeys | |
// this scans a list of things and tries to find a registry path that has Java | |
let rec scanForJava searchList = | |
match searchList with | |
| [] -> "" | |
| x::xs -> | |
let path = findJavaInRegistry x | |
if not <| String.IsNullOrEmpty path then path | |
else scanForJava xs | |
/// Attempt to discover the Java path on this local system. | |
/// It tries to use environment variables (JAVA_HOME) and the local filesystem | |
/// if this is a windows machine. | |
let private DiscoverJavaPath () = | |
let envPath = environVar "JAVA_HOME" | |
let systemPath = scanForJava ["Java Runtime Environment"; "Java Development Kit"] | |
if not <| String.IsNullOrEmpty envPath then envPath @@ "bin" @@ "java.exe" | |
elif not <| String.IsNullOrEmpty systemPath then systemPath @@ "bin" @@ "java.exe" | |
else String.Empty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment