I want to call functions in a java file using javascript . Please help me to achieve this. I went through a tutorial at
https://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html
but I couldn't understand properly attributes like code: , jnlp_href: etc. I just read and then I made minor changes in values what I felt necessary.
I have a folder wherein I have html file: index.html, jar file:Example.jar and a class file : Example.class
Following is my html code : index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<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:'app', code:'Example', width:1, height:1};
var parameters = { jnlp_href: 'Example.jnlp'};
deployJava.runApplet(attributes, parameters, '1.6');
</script>
<script language="javascript">
function openMsg()
{
var msg = app.getMessage();
document.getElementById("print").innerHTML = msg;
}
</script>
</head>
<body>
<button onclick="openMsg();">Click to open message</button>
<p id="print"></p>
</body>
</html>
My java file : Example.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class Example extends JApplet{
public void start()
{
}
public void init()
{
setBackground(Color.blue);
}
public void paint(Graphics g)
{
g.drawString("Hello World !", 100, 100);
}
public void getMessage()
{
System.out.println("Good Morning..!!!");
}
}
My objective in this program is whenever I click on the button, javascript should call getMessage() function from the jar file and then display Good Morning on the screen.
But presently I am not getting the desired results. Please help me with this..