0

I am a newbie in javascript and java programming. I have a .java file with a function updateInfo(). I want to call that function in my .js file. How can I do that? Please help. Thanks!!!

achll
  • 153
  • 1
  • 4
  • 12
  • 1
    Java is not like JavaScript, you can't call methods from plain code (`.java` file), you have to compile it first. – BackSlash Aug 03 '13 at 17:24
  • Can you show me a sample code of how to do it? Please. – achll Aug 03 '13 at 17:26
  • You can't, [unless you are using a (J)Applet](http://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html) – BackSlash Aug 03 '13 at 17:27
  • @BackSlash or if you're using Rhino. – Pointy Aug 03 '13 at 17:29
  • 1
    @Pointy yes, that's true. There are many scenarios, and we don't have much informations to determine which scenario the OP is looking for to provide a good solution – BackSlash Aug 03 '13 at 17:32
  • I am using jsp to make it possible. I did it already. – achll Aug 03 '13 at 17:37
  • 1
    @AnachelleCasandraCastillo Update your question with this detail. You are looking for an ajax solution, already posted [here](http://stackoverflow.com/questions/11536455/calling-java-method-in-javascript) – BackSlash Aug 03 '13 at 17:39

4 Answers4

1

Yes, you can, but not directly.

One option is to use a JAX-RS implementation like Apache CXF or Jersey. Once you have created a RESTful web service that maps to the method in your Java file, you can use JavaScript to make an AJAX call. Thus, you can certainly call a Java method via JavaScript in a form of a RESTful web service.

haoudoin
  • 151
  • 10
0

You can't directly call a java method from JavaScript. Equals whether the java Code is Compiled or not.

MinecraftShamrock
  • 3,504
  • 2
  • 25
  • 44
  • _Equals whether the java Code is Compiled or not_ **False.** If java code is compiled and run as an applet you can, and it's described on the [Oracle tutorials for calling applet methods from JS](http://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html) – BackSlash Aug 03 '13 at 17:29
  • It depends on the context. If you're running JavaScript in Rhino on the server side, it's perfectly possible to call Java from JavaScript. – Pointy Aug 03 '13 at 17:29
0

To bind Java code with JavaScript you must create first Applet. Only after, there are several techniques to invoke JavaScript from Applet and vice versa

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

First make sure your java is compiled to jar (and extends applet / japplet)

You can invoke javascript functions with netscape.javascript.*

Example HTML

<head>
<title>Data Summary Applet Page - Java to JavaScript LiveConnect</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<script language="javascript">
    var userName = "";

    // returns number
    function getAge() { 
        return 25;
    }
    // returns an object
    function address() { 
        this.street = "1 Example Lane";
        this.city = "Santa Clara";
        this.state = "CA";
    }
    // returns an array
    function getPhoneNums() { 
        return ["408-555-0100", "408-555-0102"];
    } 
    function writeSummary(summary) {
        summaryElem =
            document.getElementById("summary");
        summaryElem.innerHTML = summary;
    }
    </script>

    <!-- ... -->      
</head>
<body>
    <script src =
      "http://www.java.com/js/deployJava.js"></script>
    <script> 
        <!-- ... -->
        deployJava.runApplet(attributes, parameters, '1.6'); 
    </script>          
    <!-- ... -->
    <p id="summary"/>  // this HTML element contains
                             // the summary 
    <!-- ... -->
</body>

Example java implementation:

package javatojs;

import java.applet.Applet;
import netscape.javascript.*; // add plugin.jar to classpath during compilation

public class DataSummaryApplet extends Applet {
    public void start() {
        try {
            JSObject window = JSObject.getWindow(this);

            String userName = "John Doe";

            // set JavaScript variable
            window.setMember("userName", userName);

            // invoke JavaScript function
            Number age = (Number) window.eval("getAge()");

            // get a JavaScript object and retrieve its contents
            JSObject address = (JSObject) window.eval("new address();");
            String addressStr = (String) address.getMember("street") + ", " +
                    (String) address.getMember("city") + ", " +
                    (String) address.getMember("state");

            // get an array from JavaScript and retrieve its contents
            JSObject phoneNums = (JSObject) window.eval("getPhoneNums()");
            String phoneNumStr = (String) phoneNums.getSlot(0) + ", " +
                    (String) phoneNums.getSlot(1);

            // dynamically change HTML in page; write data summary
            String summary = userName + " : " + age + " : " +
                    addressStr + " : " + phoneNumStr;
            window.call("writeSummary", new Object[] {summary})   ;
        } catch (JSException jse) {
            jse.printStackTrace();
        }
    }
}

More information about invoking javascript from java

More information about invoking java methods from javascript

Kees
  • 1,408
  • 1
  • 15
  • 27