最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Running Java code from html <script>-node - Stack Overflow

programmeradmin0浏览0评论

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.)

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.)

Share Improve this question edited Dec 1, 2011 at 1:28 nnnnnn 150k30 gold badges209 silver badges247 bronze badges asked Dec 1, 2011 at 0:18 Mathias BakMathias Bak 5,1456 gold badges33 silver badges42 bronze badges 7
  • 2 Who told you that was possible? – Carsten Commented Dec 1, 2011 at 0:23
  • I guess you need to alert that inside Java, not JavaScript – Pekka Commented Dec 1, 2011 at 0:23
  • @Carsten in the ments to the original post here: stackoverflow./questions/8283502/… – Mathias Bak Commented Dec 1, 2011 at 0:25
  • @MathiasBak you didn't read all the ments obviously :) – soulcheck Commented Dec 1, 2011 at 0:26
  • @soulcheck can you tell me what I missed? That some other guys could not make it work either? – Mathias Bak Commented Dec 1, 2011 at 0:28
 |  Show 2 more ments

3 Answers 3

Reset to default 2

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 FF (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 pilable 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 mon thing between these languages is the title and naming style.

发布评论

评论列表(0)

  1. 暂无评论