Search code examples
javascriptjavaapplet

Javascript, Calling Java Applet method throws TypeError


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:

  1. Chrome 57.0.2987.133 (64-bit)
  2. Firefox 52.0.2 (32-bit)

Applet was compiled by using JDK 8.


Solution

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