简体   繁体   中英

Call Java API from javascript on the client (no web service)

I need a way to use a Java API (jar) from the javascript code on the local client. Can this be achieved and how?


Context

I have a Java API (jar file) that allows to connect to a real time information feed. You can submit a query and, for example, print the events you will receive:

service.subscribe(query, evt -> print(evt));

That API can only be used on the client machine for legal reasons so I can't expose it as a web service from a server.

Goal

I would like to create a web page that gets data from a web service and combines it with the real time information data obtained from the Java API locally.

I am using angular 2 but happy to consider any suggestions.

Web service

I have seen various similar questions but the answers tend to be: expose the API via a web service - that is not possible in my case .

You can use java applets for this purpose.

You should start by making an applet that encloses the call to your method:

public class TestApplet extends Applet{

    private ? service = ...;

    public Object subscribe(Object query) {
        return service.subscribe(query, evt -> print(evt));;
    }
}

This applet can then be included in the html of the webpage:

<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:'testApplet', code:'yourpackage.TestApplet', width:1, height:1} ;
    var parameters = {jnlp_href: 'test_applet.jnlp'} ;
    deployJava.runApplet(attributes, parameters, '1.6');
</script>

Then you can use javascript to call the methods:

var greeting = testApplet.subscribe("Test");

Note that applets are being phased out because of their security problems, but this is ok in an controlled and embedded environment.

The following oracle tutorial gives more information about this technique: Invoking Applet Methods From JavaScript Code

The only way I can think of is using the javafx webview: http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm

Basically:

  • you create your own java-based-"webbrowser" withjavafx
  • you can then expose the java api into the webview
  • you can open a normal html page (ie http://server.tld/mypage.html ) within the webview and use javascript to access the api

in the javascript you can check if the site has been opened with a normal browser or with you custom webview by checking if the exposed api is available:

the java code for something like that:

WebView webView = new WebView();

jfxPanel.setScene(new Scene(webView));
webEngine = webView.getEngine();
webEngine.setJavaScriptEnabled(true);
webEngine.setConfirmHandler(new ModalConfirmDialog(self));


// get the window and pass the required daos
JSObject jsobj = (JSObject) webEngine.executeScript("window");
// pass the dataaccess to the js context
jsobj.setMember("javaapi", getApiInstance());

webEngine.load("http://whatever.tld/mypage.html");

in javascript:

if(!window.javaapi) {
   alert("Unable to get local java api");
   return;
} 

Other possibilities:

  • Applets: they wont work because they need to be downloaded from the same source as the webpage (which you cant use because of licensing restrictions)
  • JSP/Servlet: cant be used because this means the api must reside on the server (again licensing restriction)
  • Java Javascript Engine: You can call javascript directly from java, but since you want the javascript in a webpage, this wont work either...

Simply you cannot run .jar file from java script (But you can execute from nodejs) You can use applet to do that. You can refer this link.

It might sound really weird but actually, there's such tool that enable's you to 'convert' from java to js. But of course it has it's limitations and in order to successfully apply it in your particular case w/o doing modifications and dancing with a tambourine, you should be extremely lucky . this tool is able to convert it to js and allows you to create a JS API for the converted JS, so that it can be accessible from other js scripts. What I'm talking about is GWT . You must have source files of a jar (it might be decompiled sources) including all sources of dependencies that are used by lib.

Maybe you can invest in building a bridge between your JS code and the jar using Nashorn.

It allows you to evaluate JS code and invoke JS functions from Java, so it may serve your usecase. You can build a Java layer to connect to the API from the jar and then publish the results to JS by calling some function using Nashorn.

Or you can make use of the ability to directly call Java functions from JS.

Here is a simple tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM