I have a jre folder which is basically java runtime, it is not installed I have copied this folder from somewhere else, now I need to check this jre is 32 bit or 64 bit with manual inspection without writing any code and if code has to be written then it should be c#. All the example tell system.getproperty("java...model") something for getting the target type of the installed jre, but I dont have this jre installed, rather I have just copied this jre. so is there any way to know its target type is it 32 bit or 64 bit.
How to check that a jre environment is 32 bit or 64 bit programmatically with C#?
2.9k Views Asked by Yogesh At
3
There are 3 best solutions below
3

C# Code
// *** Main code
string output = RunExternalExe("java.exe -version");
// Parse output here...
// *** Helper methods
public string RunExternalExe(string filename, string arguments = null)
{
var process = new Process();
process.StartInfo.FileName = filename;
if (!string.IsNullOrEmpty(arguments))
{
process.StartInfo.Arguments = arguments;
}
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
var stdOutput = new StringBuilder();
process.OutputDataReceived += (sender, args) => stdOutput.Append(args.Data);
string stdError = null;
try
{
process.Start();
process.BeginOutputReadLine();
stdError = process.StandardError.ReadToEnd();
process.WaitForExit();
}
catch (Exception e)
{
throw new Exception("OS error while executing " + Format(filename, arguments)+ ": " + e.Message, e);
}
if (process.ExitCode == 0)
{
return stdOutput.ToString();
}
else
{
var message = new StringBuilder();
if (!string.IsNullOrEmpty(stdError))
{
message.AppendLine(stdError);
}
if (stdOutput.Length != 0)
{
message.AppendLine("Std output:");
message.AppendLine(stdOutput.ToString());
}
throw new Exception(Format(filename, arguments) + " finished with exit code = " + process.ExitCode + ": " + message);
}
}
private string Format(string filename, string arguments)
{
return "'" + filename +
((string.IsNullOrEmpty(arguments)) ? string.Empty : " " + arguments) +
"'";
}
COMMAND sample output
On my box I have a 64bit java version. Here is its output:
C:\Program Files\Java\jdk1.7.0_45\bin>java -version java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
Note the 64-Bit mention. You may find this SO answer useful.
0

I know I am late to this, but I wrote these methods in C# so that it can detect both 32/64-bit JRE and JDK because the provided answers don't suffice my need
private string CheckJreJdkVersion()
{
StringBuilder sb = new StringBuilder();
string jre = @"SOFTWARE\JavaSoft\Java Runtime Environment";
string jdk = @"SOFTWARE\JavaSoft\Java Development Kit";
string jreInstallDirValueName = "INSTALLDIR";
string jdkInstallDirValueName = "JavaHome";
// Check 32-bit JRE
GetJreJdkVersion(jre, RegistryView.Registry32, jreInstallDirValueName, ref sb, "JRE", "32");
// Check 64-bit JRE
GetJreJdkVersion(jre, RegistryView.Registry64, jreInstallDirValueName, ref sb, "JRE", "64");
// Check 32-bit JDK
GetJreJdkVersion(jdk, RegistryView.Registry32, jdkInstallDirValueName, ref sb, "JDK", "32");
// Check 64-bit JDK
GetJreJdkVersion(jdk, RegistryView.Registry64, jdkInstallDirValueName, ref sb, "JDK", "64");
string res = sb.ToString();
return res;
}
private void GetJreJdkVersion(string jreJdkPath, RegistryView registryView, string jreJdkInstallDirValueName, ref StringBuilder sb, string jreJdk, string bitVer)
{
RegistryKey key = GetRegistryKeyHKLM(jreJdkPath, registryView);
if (key == null)
return;
List<string> lstVersions = new List<string>();
foreach (var version in key.GetSubKeyNames())
{
lstVersions.Add(version);
}
IEnumerable<string> relevantVersions = GetRelevantVersions(lstVersions);
foreach (var relevantVersion in relevantVersions)
{
string regPath = string.Empty;
if (jreJdk == "JRE")
{
regPath = Path.Combine(jreJdkPath, Path.Combine(relevantVersion, "MSI"));
}
else
{
regPath = Path.Combine(jreJdkPath, relevantVersion);
}
string installDir = GetRegistryValueHKLM(regPath, jreJdkInstallDirValueName, registryView);
sb.Append("Detected " + bitVer + "-bit " + jreJdk + ", install directory: " + installDir + "\n");
}
}
private IEnumerable<string> GetRelevantVersions(List<string> lstVersions)
{
IEnumerable<string> versions = lstVersions.Where(version => version.Contains("_"));
return versions;
}
public RegistryKey GetRegistryKeyHKLM(string keyPath, RegistryView view)
{
RegistryKey localMachineRegistry
= RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view);
return string.IsNullOrEmpty(keyPath)
? localMachineRegistry
: localMachineRegistry.OpenSubKey(keyPath);
}
public string GetRegistryValueHKLM(string keyPath, string keyName, RegistryView view)
{
RegistryKey registry = GetRegistryKeyHKLM(keyPath, view);
if (registry == null) return null;
string value = string.Empty;
try
{
value = registry.GetValue(keyName).ToString();
}
catch (Exception)
{
}
return value;
}
Sample output:
Detected 32-bit JRE, install directory: C:\Program Files (x86)\Java\jre7\
Detected 32-bit JRE, install directory: C:\Program Files (x86)\Java\jre1.8.0_73\
Detected 32-bit JRE, install directory: C:\New folder\
Detected 64-bit JRE, install directory: C:\Program Files\Java\jre7\
Detected 32-bit JDK, install directory: C:\jdk fol
You could use the GNU
file
utility to check whether or not java.exe is a 64-bit-executable.See this link for more information about that.
This way, you can avoid starting the unknown java.exe from your code, but instead your program has to start the
file
utility you have provided yourself.