简体   繁体   English

从客户端上的javascript调用Java API(无网络服务)

[英]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. 我需要一种从本地客户端上的javascript代码中使用Java API(jar)的方法。 Can this be achieved and how? 这可以实现吗?


Context 语境

I have a Java API (jar file) that allows to connect to a real time information feed. 我有一个Java API(jar文件),可以连接到实时信息提要。 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. 出于法律原因,该API只能在客户端计算机上使用,因此我无法将其作为来自服务器的Web服务公开。

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. 我想创建一个从Web服务获取数据并将其与从Java API本地获取的实时信息数据相结合的网页。

I am using angular 2 but happy to consider any suggestions. 我正在使用角度2,但很高兴考虑任何建议。

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 . 我已经看到各种类似的问题,但答案往往是:通过Web服务公开API- 在我的情况下是不可能的

You can use java applets for this purpose. 您可以将Java applet用于此目的。

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: 然后可以将此小程序包含在网页的html中:

<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: 然后,您可以使用javascript调用方法:

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 以下oracle教程提供了有关此技术的更多信息: 从JavaScript代码调用Applet方法

The only way I can think of is using the javafx webview: http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm 我能想到的唯一方法是使用Javafx Webview: http : //docs.oracle.com/javafx/2/webview/jfxpub-webview.htm

Basically: 基本上:

  • you create your own java-based-"webbrowser" withjavafx 您可以使用javafx创建自己的基于Java的“ Webbrowser”
  • you can then expose the java api into the webview 然后可以将Java api公开到webview中
  • you can open a normal html page (ie http://server.tld/mypage.html ) within the webview and use javascript to access the api 您可以在网络视图中打开普通的html页面(即http://server.tld/mypage.html ),然后使用javascript访问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: 在javascript中,您可以通过检查公开的api是否可用来检查是否已使用常规浏览器或自定义Webview打开了该网站:

the java code for something like that: 像这样的Java代码:

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: 在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) JSP / Servlet:无法使用,因为这意味着api必须驻留在服务器上(再次限制许可)
  • Java Javascript Engine: You can call javascript directly from java, but since you want the javascript in a webpage, this wont work either... Java Javascript Engine:您可以直接从Java调用Javascript,但是由于您希望在网页中使用Javascript,因此该方法也不起作用。

Simply you cannot run .jar file from java script (But you can execute from nodejs) You can use applet to do that. 简而言之,您无法从Java脚本运行.jar文件(但可以从nodejs执行)。您可以使用applet来执行此操作。 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. 听起来确实很奇怪,但是实际上,有这样的工具可以使您从Java转换为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. 该工具能够将其转换为js,并允许您为转换后的JS创建JS API,以便可以从其他js脚本中访问它。 What I'm talking about is GWT . 我在说的是GWT You must have source files of a jar (it might be decompiled sources) including all sources of dependencies that are used by lib. 您必须具有jar的源文件(它可能是反编译的源),包括lib使用的所有依赖项源。

Maybe you can invest in building a bridge between your JS code and the jar using Nashorn. 也许您可以投资使用Nashorn在JS代码和jar之间建立桥梁。

It allows you to evaluate JS code and invoke JS functions from Java, so it may serve your usecase. 它允许您评估JS代码并从Java调用JS函数,因此它可以满足您的用例。 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. 您可以构建Java层以从jar连接到API,然后通过使用Nashorn调用某些函数将结果发布到JS。

Or you can make use of the ability to directly call Java functions from JS. 或者,您可以利用直接从JS调用Java函数的功能。

Here is a simple tutorial 这是一个简单的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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