20

i created a java class content method return a String, my question is how to call this function in my javascript code to use the returned value from the java method. I want to call client-side Java code embedded in browser.

here is an exemple of what im talking about:

in my webpage i have a javascript code, here is some of it:

    function createChartControl(htmlDiv1)
{
    // Initialize Gantt data structures
    //project 1
    var parentTask1 = new GanttTaskInfo(1, "Old code review", new Date(2010, 5, 11), 208, 50, "");
......................

i want to create a java class content methods to provide data to this javascript function "GanttTaskInfo". for exemple function to get name, get id and date. well i think this time im clear :D i searched a way to call java methods in javascript, and i found applets as you said, but i think its not usefull to me. thanks again

Nadya Nux
  • 519
  • 1
  • 5
  • 17
  • 1
    The question needs clarification. – Martin Peters Jul 11 '11 at 11:05
  • 3
    What Java code? Applet? Web service? JSP? What JavaScript code? In a browser? Node.js? How are you expecting the data to go between the two? What computers are they running on? – Quentin Jul 11 '11 at 11:05
  • This is apples and oranges. Java and Javascript have no inherent relationship, they are just two languages that have somewhat similar syntax and (unfortunately) similar names. You need to provide much more information about the context of your problem. Is this a servlet running on an application server or is it an applet? Is the Javascript executed as part of a web page generated by the servlet or is it server side javascript? – pap Jul 11 '11 at 11:16
  • 3
    Why closed? I think this is quite nice question. And solution is supported by Oracle Java Plugin. Look at my answer :) – zacheusz Jul 11 '11 at 11:34
  • 1
    Though, the question is ambiguous, you could let him improve. One way to call java from javascript is using _Java Scripting Engine_ – Op De Cirkel Jul 11 '11 at 11:47
  • 9
    +1 to re-open the question – Op De Cirkel Jul 11 '11 at 11:57

2 Answers2

27

When it is on server side, use web services - maybe RESTful with JSON.

  • create a web service (for example with Tomcat)
  • call its URL from JavaScript (for example with JQuery or dojo)

When Java code is in applet you can use JavaScript bridge. The bridge between the Java and JavaScript programming languages, known informally as LiveConnect, is implemented in Java plugin. Formerly Mozilla-specific LiveConnect functionality, such as the ability to call static Java methods, instantiate new Java objects and reference third-party packages from JavaScript, is now available in all browsers.

Below is example from documentation. Look at methodReturningString.

Java code:

public class MethodInvocation extends Applet {
    public void noArgMethod() { ... }
    public void someMethod(String arg) { ... }
    public void someMethod(int arg) { ... }
    public int  methodReturningInt() { return 5; }
    public String methodReturningString() { return "Hello"; }
    public OtherClass methodReturningObject() { return new OtherClass(); }
}

public class OtherClass {
    public void anotherMethod();
}

Web page and JavaScript code:

<applet id="app"
        archive="examples.jar"
        code="MethodInvocation" ...>
</applet>
<script language="javascript">
    app.noArgMethod();
    app.someMethod("Hello");
    app.someMethod(5);
    var five = app.methodReturningInt();
    var hello = app.methodReturningString();
    app.methodReturningObject().anotherMethod();
</script>
Michael
  • 41,989
  • 11
  • 82
  • 128
zacheusz
  • 8,750
  • 3
  • 36
  • 60
-6

Java is a server side language, whereas javascript is a client side language. Both cannot communicate. If you have setup some server side script using Java you could use AJAX on the client in order to send an asynchronous request to it and thus invoke any possible Java functions. For example if you use jQuery as js framework you may take a look at the $.ajax() method. Or if you wanted to do it using plain javascript, here's a tutorial.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 19
    No, [Java is a client side language](http://java.sun.com/applets/) while [JavaScript is a server side language](http://en.wikipedia.org/wiki/Server-side_JavaScript). – Quentin Jul 11 '11 at 11:06
  • 1
    They can - I think Nadya might mean the scenario of a scriptable applet where Javascript can indeed call into Java world and vice versa. But anyway the question is too detailed to know ;) – emboss Jul 11 '11 at 11:07
  • 1
    Yes, applets still exist, I make use of one on a weekly basis (although I'd rather it wasn't implemented that way). – Quentin Jul 11 '11 at 11:09
  • @Darin: Unfortunately they're the only thing compatible across browsers that lets you access a PKCS#11-compatible smart card in a web app. Shame on the browser vendors/makers for not integrating this directly in Javascript! – emboss Jul 11 '11 at 11:11
  • 1
    sorry if my question is not clear because i still beginer,i'll explain it, i have a java class with methods, and in my pageweb i have a javascript code. i want to call my methods in javascript code without using applets. – Nadya Nux Jul 11 '11 at 12:03
  • @Darin Dimitrov :thanks your answer – Nadya Nux Jul 11 '11 at 12:07
  • Create a JavaScriptInterface and write that function in that and then in javascript call that method like this : window.jsinterface.showViewerInterface() – Prax Oct 03 '12 at 11:49
  • It wrong to say that Java is only server side language and js is only client side language – Hari Ram Mar 27 '17 at 11:25