Reputation: 665
I am writing my first C# program and I want to check using C# code if I am running a 32 or 64 bit version of java ?
I tried this but when I add this code to my class I am not able to debug it
RegistryKey rk = Registry.LocalMachine;
RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment");
string currentVerion = subKey.GetValue("CurrentVersion").ToString();
How can I do it ?
Thanks
Upvotes: 1
Views: 1188
Reputation: 79
I`m use this code:
public static bool CheckJavaInstallation()
{
try
{
//ProcessStartInfo procStartInfo = new ProcessStartInfo("java", " -version"); // Check that any Java installed
//ProcessStartInfo procStartInfo = new ProcessStartInfo("java", "-d32 -version"); // Check that 32 bit Java installed
ProcessStartInfo procStartInfo = new ProcessStartInfo("java", "-d64 -version"); // Check that 64 bit Java installed
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process {StartInfo = procStartInfo};
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
return proc.ExitCode == 0;
}
catch (Exception ex)
{
return false;
}
}
Upvotes: 0
Reputation: 1012
You can do it through the registry. I knocked together a quick example for you:
private string GetJavaInstallationPath()
{
string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
if (!string.IsNullOrEmpty(environmentPath))
{
return environmentPath;
}
string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}
Then to use it, just do the following:
string installPath = GetJavaInstallationPath();
string filePath = System.IO.Path.Combine(installPath, "bin\\Java.exe");
if (System.IO.File.Exists(filePath))
{
// We have a winner
}
Upvotes: 0
Reputation: 19171
It isn't clear how you are going to identify which java.exe you are using - a single machine can have many installed. You may have a specific path, or you may need to either use the JAVA_HOME
environment variable, or search PATH
, or do a combination of both and give priority to one or the other depending on your requirements.
Once you've got your path to java.exe
you can use the technique from Kris Stanton on MSDN (which I will repeat here, but is currently linked at MSDN > "Exploring pe file headers using managed code"):
public enum MachineType
{
Native = 0, I586 = 0x014c, Itanium = 0x0200, x64 = 0x8664
}
public static MachineType GetMachineType(string fileName)
{
// dos header is 64 bytes
// PE header address is 4 bytes
const int PE_PTR_OFFSET = 60;
const int MACHINE_OFFSET = 4;
byte[] data = new byte[4096];
using (Stream stm = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
stm.Read(data, 0, 4096);
}
int PE_HDR_ADDR = BitConverter.ToInt32(data, PE_PTR_OFFSET);
int machineUint = BitConverter.ToUInt16(data, PE_HDR_ADDR + MACHINE_OFFSET);
return (MachineType)machineUint;
}
To find java.exe
on the %PATH%
variable, you can call FindOnPath("java.exe")
:
public static String FindOnPath(string exeName)
{
foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(';'))
{
string path = test.Trim();
if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, exeName)))
return Path.GetFullPath(path);
}
return null;
}
On my machine, the following code
static void Main(string[] args)
{
String path = FindOnPath("java.exe");
Console.WriteLine(path);
Console.WriteLine(GetMachineType(path));
}
writes the following output:
C:\ProgramData\Oracle\Java\javapath\java.exe
x64
Upvotes: 2