您是否可以编写Java代码来检查本地Internet Explorer的版本

时间:2012-01-18 21:24:06

标签: java internet-explorer

基本上,我只需要编写一个简单的java程序来检测我本地安装的Internet Explorer的版本。

有javascript代码,但它在您的浏览器中运行。我想要的是这样的代码:

public class VersionTest
{
   public static void main(String[] args)
   {    System.out.println("you IE Version is:" + getIEVersion());
   }

   public static String getIEVersion()
   {   //implementation that goes out and find the version of my locally installed IE
   }
}

我该怎么做?感谢

3 个答案:

答案 0 :(得分:4)

您可以使用Internet Explorer Registry Entry版本。您可以使用Runtime类从java执行Reg Query。 Reg Query是一个命令行工具,用于查询Windows中的注册表项。

Process p = Runtime.getRuntime().exec("reg query \"HKLM\\Software\\Microsoft\\Internet Explorer\" /v Version");

完整代码:

ArrayList<String> output = new ArrayList<String>()
Process p = Runtime.getRuntime().exec("reg query \"HKLM\\Software\\Microsoft\\Internet Explorer\" /v Version");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()),8*1024);
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()))
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
output.add(s)

String internet_explorer_value = (output.get(2));
String version = internet_explorer_value.trim().split("   ")[2];
System.out.println(version);

输出= 9.0.8112.16421

在我的命令提示符下输出reg query

  

HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Internet Explorer

     

版本REG_SZ 9.0.8112.16421

答案 1 :(得分:2)

您正在寻找的注册表项位于:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Version

如何阅读Java中的注册表项? Go read this Stack Overflow question.

答案 2 :(得分:2)

private String getBrowserType(String currValue){
String browser = new String("");
String version = new String("");
if(currValue != null ){
if((currValue.indexOf("MSIE") == -1) && (currValue.indexOf("msie") == -1)){
browser = "NS";
int verPos = currValue.indexOf("/");
if(verPos != -1)
version = currValue.substring(verPos+1,verPos + 5);
}
else{
browser = "IE";
String tempStr = currValue.substring(currValue.indexOf("MSIE"),currValue.length());
version = tempStr.substring(4,tempStr.indexOf(";"));

}

}
System.out.println(" now browser type is " + browser +" " + version);

return browser + " " + version;

}

Source