0

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

Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29
Mohit Gamot
  • 19
  • 1
  • 7
  • have a look at this: [calling java method in javascript](http://stackoverflow.com/questions/6649125/calling-java-methods-in-javascript-code) – Kevin Kloet Oct 17 '16 at 11:55
  • @KevinKloet Thanks for the link but still I can't resolve my issue. Whenever I click on the button I get an error in the console saying " app.getMessage is not a function " and whenever I try this using chrome , the moment browser opens ,the jar file immediatley gets downloaded. and last I would like to say is that now applet tag is obsolete so I am using object tag . – Mohit Gamot Oct 17 '16 at 12:17
  • can you upload the javascript you're using – Kevin Kloet Oct 17 '16 at 12:43
  • @KevinKloet Its already there in the above html code. Please have a look. – Mohit Gamot Oct 17 '16 at 12:51
  • if this is the newest javascript you're using, it doesn't work because app is undefined. – Kevin Kloet Oct 17 '16 at 12:56
  • also, here is the documentation on this feature: [LiveConnect](http://www.oracle.com/technetwork/java/javase/overview/liveconnect-docs-349790.html#FIELD_ACCESS) – Kevin Kloet Oct 17 '16 at 13:01
  • @KevinKloet very sorry . Please refresh the page. I have made the changes and I tried but still I get an error in the console saying that " TypeError: app.getMessage is not a function" .. Actually I don't understand what does code , jnlp_href, actually means and I just made the above changes. Could you please tell me what exactly I should do..? How to create jnlp file? – Mohit Gamot Oct 17 '16 at 13:03
  • add an archive:example.jar in the attributes variable, than create a jnlp file and tell me if it works the way it should. some explanation about jnlp here [appletjnlp](https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/runAppletFunction.html#appletJnlp) and here [create jnlp file](http://www.mkyong.com/java/java-web-start-jnlp-tutorial-unofficial-guide/) – Kevin Kloet Oct 17 '16 at 13:19
  • @KevinKloet Thanks a lot. but is it necessary to create .jnlp file..? can I send empty parameter ? – Mohit Gamot Oct 17 '16 at 13:55
  • the jnlp file(Java Network Launch Protocol) is used for configurations on how the applet should be launched, i don't think works without the jnlp file. even if it did work i still recommend you using the jnlp file. – Kevin Kloet Oct 17 '16 at 14:15

0 Answers0