1

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.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I don't think this is possible, because Java has full access to the underlying machine's resources which is prohibited for JavaScript to access to. – frogatto Feb 12 '16 at 10:08
  • You could install the server on the client so it doesn't have to go to the outside :/ Other than that, you're shoehorned. You can't program yourself through a brick wall, you need a doorway first. – Gimby Feb 12 '16 at 11:44
  • Yes I thought of that but I am not going to install a server on each client... I see what you mean. – assylias Feb 12 '16 at 11:45
  • How do you want people starting the app, do you want them to use their browser to start a html page, or want them to open a jar file? – Ferrybig Feb 12 '16 at 11:58
  • Ideally just open a web page – assylias Feb 12 '16 at 12:05

5 Answers5

2

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

Ferrybig
  • 18,194
  • 6
  • 57
  • 79
1

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 (i.e. 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...
Niko
  • 6,133
  • 2
  • 37
  • 49
  • Thanks for your answer - I know webview but would prefer it if the users could use their "normal" browser. – assylias Feb 12 '16 at 10:29
  • Unfortunately there is no valid way to call java from a normal browser unless with an applet or server side - and all of these require you to distribute the library onto the server – Niko Feb 12 '16 at 10:40
1

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.

Community
  • 1
  • 1
1

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.

Yogesh
  • 1,565
  • 1
  • 19
  • 46
hahn
  • 3,588
  • 20
  • 31
0

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

Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28