如何从 ASP .NET 网站检测客户端上安装的 Java 运行时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/491541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I detect the Java runtime installed on a client from an ASP .NET website?
提问by Carl
I have an ASP .NET website that hosts a Java applet. The Java applet requires version 1.6 Update 11 of the Java runtime.
我有一个托管 Java 小程序的 ASP .NET 网站。Java 小程序需要 Java 运行时的 1.6 Update 11 版。
How can I detect that a client has the appropriate runtime installed so that I can display an informative message if they do not?
如何检测客户端是否安装了适当的运行时,以便在没有安装时显示信息性消息?
Thanks,
谢谢,
Carl.
卡尔。
EDIT:The solution must be platform independant.
编辑:解决方案必须与平台无关。
回答by Andreas Grech
This page describes how to and lists some plugins that will allow you to detect Java with JavaScript: http://www.pinlady.net/PluginDetect/JavaDetect.htm
此页面描述了如何并列出了一些允许您使用 JavaScript 检测 Java 的插件:http: //www.pinlady.net/PluginDetect/JavaDetect.htm
Other than that, try out this snippet as well:
除此之外,也试试这个片段:
if (navigator.javaEnabled()) {
//Java is enabled
}
回答by Ramesh
The link below details on the deployment tips for java apps.
下面的链接详细介绍了 Java 应用程序的部署技巧。
http://java.sun.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html
http://java.sun.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html
Quoting from the link
从链接引用
Deployment Toolkit To avoid browser compatibility issues, the Deployment Toolkit (deployJava.js) provides JavaScript functions that automatically generate HTML required to deploy applets and Java Web Start applications. Developers should invoke these functions to deploy their solutions in a consistent fashion across various browsers.
部署工具包 为了避免浏览器兼容性问题,部署工具包 ( deployJava.js) 提供了 JavaScript 函数,可以自动生成部署小程序和 Java Web Start 应用程序所需的 HTML。开发人员应该调用这些函数以在各种浏览器中以一致的方式部署他们的解决方案。
回答by paxdiablo
My approach would be to use the JavaScript navigator.javaEnabled()to check if there is someJava version available.
我的方法是使用JavaScriptnavigator.javaEnabled()来检查是否有一些可用的Java版本。
Then you can use System.getProperty("java.version")from within a Java applet itself. That should be enough to get you the version information, such as 1.6.0_03.
然后您可以System.getProperty("java.version")在 Java 小程序本身中使用。这应该足以让您获得版本信息,例如1.6.0_03.
回答by Andrew Arnott
Although most of the answers so far are all around detection on the user agent (browser) itself, it sounds like from your mention of ASP.NET that you'd like to make this detection happen on the server-side. If so, you have a couple of options.
尽管到目前为止的大多数答案都是围绕用户代理(浏览器)本身的检测,但从您提到的 ASP.NET 中,您似乎希望在服务器端进行这种检测。如果是这样,您有几个选择。
First you can sniff the HTTP request headers coming from the user agent. A computer and browser with Java installed will usually include a header providing a hint of this to the server that you can pick up on. Here are some useful links on this approach: http://www.developershome.com/wap/detection/https://metacpan.org/pod/HTTP::Browscap
首先,您可以嗅探来自用户代理的 HTTP 请求标头。安装了 Java 的计算机和浏览器通常会包含一个标题,向您可以获取的服务器提供此提示。以下是有关此方法的一些有用链接:http: //www.developershome.com/wap/detection/ https://metacpan.org/pod/HTTP::Browscap
The other option you have is to send down javascript to the user agent to perform the detection using one of the techniques in the other answers in this question and then use ajax callbacks to tell the server what you discovered.
您拥有的另一个选择是将 javascript 发送到用户代理,以使用本问题其他答案中的一种技术执行检测,然后使用 ajax 回调告诉服务器您发现了什么。
回答by achinda99
If you don't mind using a basic java applet that requires a much older version to run, you could use this articleand continue based on that result.
如果您不介意使用需要更旧版本才能运行的基本 Java 小程序,您可以使用本文并根据该结果继续。
回答by Gili
Use deployJava.jsfunction getJREs() to build a dynamic page depending on the user's Java version.
Use http://cowwoc.blogspot.com/2008/12/tracking-java-versions-using-google.htmlto track the Java version being used by your web visitors using Google Analytics.
Bonus step: use the source-code from step 2as an example of how step 1 should be implemented.
使用deployJava.js函数 getJREs() 根据用户的 Java 版本构建动态页面。
使用http://cowwoc.blogspot.com/2008/12/tracking-java-versions-using-google.html使用 Google Analytics 跟踪您的网络访问者正在使用的 Java 版本。
额外步骤:使用步骤 2 中的源代码作为应如何实施步骤 1 的示例。
回答by Matias
Just copy amd paste this code in your browser address bar! Check if it is useful for you.
只需复制并粘贴此代码到您的浏览器地址栏中即可!检查它是否对您有用。
javascript: for(i = 0; i < navigator.plugins.length; i++) alert(navigator.plugins[i].name);
You need to obtain the position of Java in the plugins array and maybe you can check the versions with a regular expression or something. You could take a look to the plugin javascript object.
您需要获取 Java 在 plugins 数组中的位置,也许您可以使用正则表达式或其他方式检查版本。您可以查看插件 javascript 对象。
Regards!
问候!
回答by Dennis C
If the solution must be platform independent. I'll exclude all solution provided by javascript.
如果解决方案必须独立于平台。我将排除 javascript 提供的所有解决方案。
You can write a very simple Applet which support by the oldish Classic VM. Let the applet detect the JRE version, and display messsage.
您可以编写一个非常简单的 Applet,它由旧的 Classic VM 支持。让小程序检测 JRE 版本,并显示消息。
If you accept Javascript, you may also interest to this articlewhich is about auto-installer for WebStart applications.
回答by SquidScareMe
Maybe this script will help. This is Windows-only, though.
也许这个脚本会有所帮助。不过,这仅适用于 Windows。
<script language='javascript' type='text/javascript'>
var javaVersion;
var shell;
try
{
// Create WSH(WindowsScriptHost) shell, available on Windows only
shell = new ActiveXObject("WScript.Shell");
if (shell != null)
{
// Read JRE version from Window Registry
try
{
javaVersion = shell.regRead("HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\");
}
catch(e)
{
// handle exceptions raised by 'shell.regRead(...)' here
// so that the outer try-catch block would receive only
// exceptions raised by 'shell = new ActiveXObject(...)'
alert('error reading registry');
}
}
}
catch(e)
{
// Creating ActiveX controls thru script is disabled
// in InternetExplorer security options
// To enable it:
// a. Go to the 'Tools --> Internet Options' menu
// b. Select the 'Security' tab
// c. Select zone (Internet/Intranet)
// d. Click the 'Custom Level..' button which will display the
// 'Security Settings' window.
// e. Enable the option 'Initialize and script ActiveX controls
// not marked as safe'
activeXDisabled = true;
}
// Check whether we got required (1.6) Java Plugin
if ( javaVersion != null && javaVersion.indexOf("1.6"))
{
pluginDetected = true;
alert('it is installed!')
}
if (pluginDetected)
{
// show applet page
}
else if (confirm("Java Plugin 1.6 not found"))
{
// show install page
alert('not found')
}
else
{
// show error page
alet('error')
}
</script>

