Created
April 13, 2020 13:17
-
-
Save sandrock/358bdd579b9decc64ca58c82e3ee23ef to your computer and use it in GitHub Desktop.
Java Runtime Check Task
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
namespace Somewhere | |
{ | |
using System; | |
using System.IO; | |
using Microsoft.Win32; | |
internal sealed class JavaRuntimeCheckTask | |
{ | |
public void Verify(PackageContext context) | |
{ | |
var layer = new JavaRuntimeLayer(); | |
// NOTE: this only works on Windows | |
var localMachineRegistry = Registry.LocalMachine; | |
if (SearchRegistry(layer, RegistryView.Registry64)) | |
{ | |
// found x64 JRE | |
} | |
else if (SearchRegistry(layer, RegistryView.Registry32)) | |
{ | |
// found x86 JRE | |
} | |
else | |
{ | |
// found no JRE | |
} | |
int errors = 0; | |
if (layer.Error != null) | |
{ | |
if (this.FailPolicy == PackageManifestInstallTaskFailPolicy.Terminate || this.FailPolicy == PackageManifestInstallTaskFailPolicy.Ask) | |
{ | |
errors++; | |
interactor.WriteTaskError(this, layer.Error); | |
} | |
else | |
{ | |
interactor.WriteTaskInfo(this, layer.Error); | |
} | |
} | |
else | |
{ | |
interactor.WriteTaskInfo(this, "Found Java " + layer.JavaVersion + " installed at \"" + layer.JavaHome + "\". "); | |
} | |
context.Verified(this, errors == 0); | |
} | |
private static bool SearchRegistry(JavaRuntimeLayer layer, RegistryView mode) | |
{ | |
layer.Clear(); | |
var localMachineRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, mode); | |
layer.Is64Bit = mode == RegistryView.Registry64; | |
layer.Is32Bit = mode == RegistryView.Registry32; | |
if (localMachineRegistry != null) | |
{ | |
var jreKey = localMachineRegistry.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment"); | |
if (jreKey != null) | |
{ | |
var jreVersionValue = jreKey.GetValue("CurrentVersion"); | |
if (jreVersionValue != null) | |
{ | |
layer.JavaVersion = jreVersionValue.ToString(); | |
var jreCurrentKey = jreKey.OpenSubKey(layer.JavaVersion); | |
if (jreCurrentKey != null) | |
{ | |
var jrePath = jreCurrentKey.GetValue("JavaHome"); | |
if (jrePath != null) | |
{ | |
layer.JavaHome = jrePath.ToString(); | |
var javaExePath = Path.Combine(layer.JavaHome, "bin", "java.exe"); | |
if (File.Exists(javaExePath)) | |
{ | |
// ok! | |
} | |
else | |
{ | |
layer.Error = "No java executable in java home \"" + layer.JavaHome + "\". "; | |
} | |
} | |
else | |
{ | |
layer.Error = "Java " + layer.JavaVersion + " looks to be installed but it is not (no JavaHome registry key). "; | |
} | |
} | |
else | |
{ | |
layer.Error = "Java " + layer.JavaVersion + " looks to be installed but it is not (no version registry key). "; | |
} | |
} | |
else | |
{ | |
layer.Error = "No Java JRE looks to be installed (even with a few registry keys present). "; | |
} | |
} | |
else | |
{ | |
layer.Error = "No Java JRE looks to be installed. "; | |
} | |
} | |
else | |
{ | |
layer.Error = "Windows Registry is not available. "; | |
} | |
return layer.Error == null; | |
} | |
} | |
internal sealed class JavaRuntimeLayer | |
{ | |
public JavaRuntimeLayer() | |
{ | |
} | |
public string Error { get; internal set; } | |
public string JavaVersion { get; internal set; } | |
public string JavaHome { get; internal set; } | |
public bool Is64Bit { get; internal set; } | |
public bool Is32Bit { get; internal set; } | |
internal void Clear() | |
{ | |
this.Error = null; | |
this.JavaVersion = null; | |
this.JavaHome = null; | |
this.Is32Bit = this.Is64Bit = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment