2013-12-19 42 views
1

我有一個jre文件夾,它基本上是java運行時,它沒有安裝我從其他地方複製了這個文件夾,現在我需要檢查這個jre是32位或64位手動檢查而無需編寫任何代碼,如果代碼有要寫的話應該是c#。 所有的例子都告訴system.getproperty(「java ... model」)來獲取已安裝的jre的目標類型,但是我沒有安裝這個jre,而是我剛纔複製了這個jre。 所以有什麼辦法可以知道它的目標類型是3​​2位還是64位。如何使用C#以編程方式檢查jre環境是32位還是64位?

+3

... – WarrenFaith

+0

檢查版本(32比64),你已經在系統上覆制它。 –

回答

1

C#代碼

// *** 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) + 
     "'"; 
} 


命令的輸出範例
在我的箱子我有一個64位的Java版本。下面是它的輸出:

 
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) 

注意64位提。你可能會發現這個SO answer有用。

+0

所以如果我在CMD中鍵入java.exe -version,我可以看到輸出。但是如果我運行你寫的代碼,我得到一個異常說java.exe沒有找到。直接運行CMD和Process.Start有什麼不同嗎? – HoKy22

+0

@ Hoky22檢查Java是安裝在您的盒子上。如果這樣檢查,可以在PATH環境變量中訪問。最後檢查,編輯代碼並將絕對路徑添加到java.exe。 – Stephan

+0

Java已安裝並在Path環境變量中設置。我試圖不去硬編碼絕對路徑,對我來說,它是在C:\ ProgramData \ Oracle \ Java \ javapath我懷疑你可以在其他地方安裝它 – HoKy22

0

您可以使用GNU file實用程序檢查java.exe是否爲64位可執行文件。

有關更多信息,請參閱this link

這樣,您可以避免從代碼中啓動未知的java.exe,但是您的程序必須啓動您自己提供的file實用程序。

0

我知道我遲到了這一點,但我寫在C#這些方法,以便它可以同時檢測六十四分之三十二位JRE和JDK,因爲提供的答案是不夠的我需要

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

輸出示例:即使沒有安裝,在bin文件夾的Java可執行文件應爲`Java的-version`反應

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 
相關問題