Running Java code from html -node

I have been told that the code

<script> alert(java.lang.System.getProperty("java.version")); </script>

should alert the java version installed in the browser.

But when I run the code, I get Uncaught ReferenceError: java is not defined.

I tested it in Google Chrome 15, and I am sure that i have java installed. I run a lot of applets daily.

Is the code valid, and if so, what can I do to make it run?

More generally, is it possible to run arbitrary Java code within <script> tags? (Note: I know that Java and JavaScript are different languages.)


You can't run Java code like that in your script. But there are other ways of getting the Java version. For example looking at the source code of this page, you can see how to list all the browser plugins. You can probably find what you need (ie java version) in all this.


I was wrong. :p

What fooled me is that this HTML:

<html> <body> <script type='text/javascript'> if (typeof java === 'undefined') { document.write("Java is not installed."); } else { document.write("java.version " + java.lang.System.getProperty("java.version")); } </script> </body> </html>

..when loaded in <strong>FF</strong> (6.0.1) will produce something like.

java.version 1.6.0_20

Or if loaded in another FF with no Java installed.

Java is not installed.

But just testing it in IE in both situations (Java & no Java) results in:

Java is not installed.

So it only works with 1 out of two of the major browsers (at the moment, as opposed to next month when it might break), and is effectively useless.

Sorry for the misinformation.


Basically what have you been told is not correct.

You're trying to use JavaScript code. JavaScript is not Java (names are bit ambiguous) and you should not miss the difference.

Java is compilable language which can be used in browsers only in form of java applets which could be embedded into a page using Java-plugin (something like Adobe Flash plugin).

JavaScript is a scripting language supported by almost all browsers.

The only common thing between these languages is the title and naming style.

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Running Java code from html -node