I have a simple Applet class:
package com.myapp;
class MyApplet extends Applet {
public String myMethod() {
return "Hello";
}
}
I have compiled my with other my java classes into jarfile. Then I have run this jar files main class to ensure that jar is correctly created. It works.
Then I try to embed this Applet into my page:
<script type="application/javascript">
$(document).ready(function () {
console.log(document.MyApplet.myMethod())
});
</script>
<applet archive="myjar.jar" code="com.myapp.MyApplet.class" id="MyApplet" name="MyApplet" width=100 height=100></applet>
This throws me this error:
Uncaught TypeError: Cannot read property 'myMethod' of undefined
Which means that document.MyApplet
returns undefined
When I try to call it like this:
document.getElementById('MyApplet').myMethod();
It throws me this:
Uncaught TypeError: document.getElementById(...).myMethod is not a function
Did I miss something?
I have tested this on:
Applet was compiled by using JDK 8
.
Try
console.log(document.getElementById('MyApplet').myMethod())
You can grab elements by getElementById
According to the docs you need to include the following script to run the applet.
<script src=
"https://www.java.com/js/deployJava.js"></script>
<script>
<!-- applet id can be used to get a reference to
the applet object -->
var attributes = { id:'MyApplet',
code:'jcom.myapp.MyApplet.class', width:1, height:1} ;
var parameters = { jnlp_href: 'MyApplet.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.6');
</script>
The main problem behind this task was that Last versions of Firefox, Chorme and Safari does not support applets and they wont launch them
The only browser that is still can launch applets is Internet Explorer and it also requires explicit permissions which are considered very dangerous.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.