Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查在C中运行的是32位还是64位java#_Java_C# - Fatal编程技术网

如何检查在C中运行的是32位还是64位java#

如何检查在C中运行的是32位还是64位java#,java,c#,Java,C#,我正在编写我的第一个C#程序,我想检查一下是否使用C#代码运行32位或64位版本的java 我尝试过这个,但是当我将这个代码添加到我的类中时,我无法调试它 RegistryKey rk = Registry.LocalMachine; RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment"); string currentVerion = subKey.GetValue("CurrentV

我正在编写我的第一个C#程序,我想检查一下是否使用C#代码运行32位或64位版本的java

我尝试过这个,但是当我将这个代码添加到我的类中时,我无法调试它

RegistryKey rk = Registry.LocalMachine;
RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment");
string currentVerion = subKey.GetValue("CurrentVersion").ToString();
我怎么做


谢谢

现在还不清楚如何识别您正在使用的java.exe-一台机器可以安装多个。您可能有一个特定的路径,或者您可能需要使用
JAVA_HOME
环境变量,或者搜索
path
,或者根据您的需求将两者结合使用,并优先考虑其中一个

一旦你找到了
java.exe
的路径,你就可以在MSDN上使用Kris Stanton的技术(我将在这里重复,但目前链接到):

要在
%PATH%
变量上查找
java.exe
,可以调用
FindOnPath(“java.exe”)

在我的机器上,以下代码

static void Main(string[] args)
{
    String path = FindOnPath("java.exe");
    Console.WriteLine(path);
    Console.WriteLine(GetMachineType(path));
}
写入以下输出:

C:\ProgramData\Oracle\Java\javapath\java.exe
x64

你可以通过注册表来完成。我为你简单地列出了一个例子:

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();
        }
    }
}
然后,要使用它,只需执行以下操作:

string installPath = GetJavaInstallationPath();
string filePath = System.IO.Path.Combine(installPath, "bin\\Java.exe");
if (System.IO.File.Exists(filePath))
{
    // We have a winner
}
我正在使用这个代码:

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;
    }
}

如果您仅在Windows上运行,可以尝试运行命令提示符
java-version
可能安装了多个JRE。如何从C#code运行java-version并获得结果“我无法调试它”这意味着什么?
System.Diagnostics.Process.Start(“CMD.exe”,“java-version”)我只需要知道当前正在使用的java安装是32位还是64位nothingmore@wearybands-这正是该代码的功能。首先找到java.exe的路径,然后查询该文件以了解其编译目的。此计算机类型用于什么?我只运行Windows,而不是在控制台上编写,如何将其存储在字符串变量中?@wearybands-
i586
x64
就是这样。至于“如何在字符串变量中存储[任何东西]”——恐怕这听起来像是另一个问题。我建议你搜索答案,或者问一个新问题——每个帖子设计一个特定的问题。
string installPath = GetJavaInstallationPath();
string filePath = System.IO.Path.Combine(installPath, "bin\\Java.exe");
if (System.IO.File.Exists(filePath))
{
    // We have a winner
}
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;
    }
}