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 bines 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.
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 bines 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.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Feb 12, 2016 at 10:00 assyliasassylias 329k83 gold badges677 silver badges801 bronze badges 5- 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 Commented Feb 12, 2016 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 Commented Feb 12, 2016 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 Commented Feb 12, 2016 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 Commented Feb 12, 2016 at 11:58
- Ideally just open a web page – assylias Commented Feb 12, 2016 at 12:05
5 Answers
Reset to default 2You 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./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./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...
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 depiled 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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745305508a4621685.html
评论列表(0条)