I'm currently using the javax.script package for interpreting and executing Javascript code on the fly. Incidentally, this is the same engine used by Mozilla's Rhino JS interpreter.
Question: is there any Java-based Javascript Engine that is faster than Rhino?
In Java 8, Oracle plan to release Nashorn, which is their own JavaScript engine built into Java. It plans to be much faster than Rhino, and aims to be the reference implementation of JavaScript in Java for the future.
See a recent interview about it with Oracle # DZone:
If you are happy to use a pre-release JDK, you can download it here.
I haven't heard of any Rhino alternatives. According to this list: http://en.wikipedia.org/wiki/List_of_ECMAScript_engines, Rhino is the only Java-based JavaScript interpeter.
javax.script is way older (and slightly modified) than the current version of Mozilla's Rhino which you can download from the Internet. Have you tried getting the latest Rhino and using that?
Also depending on for what you're using it and what kind of integration you require, using node.js may be more suitable for you.
Related
I'm not asking what the alternatives are. But, what's the "official" line on what to do about Java Nashorn deprecation now (December 2019). Are you supposed to just wait. Is graalvm to become part of the JDK or are the parts of it that are needed for Javascript support going to be part of the JDK or what? Am I going to need to pay for graalvm enterprise (just to have Javascript like I had before through Nashorn in a closed source application) is that where Oracle is taking this?
WebView has built-in javascript engine. What engine is used for it? Is it the same for openjdk and oracle jdk? May it be used separately from WebView, just as JSR 223 engine?
JavaScript Runtimes in the Oracle JRE
The full Oracle Java Runtime 8 ships with two JavaScript engines:
Nashorn: "Nashorn's goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. This Project intends to enable Java developers embedding of JavaScript in Java applications via JSR-223 and to develop free standing JavaScript applications using the jrunscript command-line tool."
JavaScriptCore: The JavaScript engine built into the WebKit implementation wrapped by WebView component the JavaFX system.
JavaScript Runtime Used by WebView and JavaFX applications
JavaFX Webkit does not use Nashorn, it uses JavaScriptCore.
You can use Nashorn to program JavaFX applications and APIs (as an alternative to programming in Java) and you can use Nashorn as a JavaFX script engine for JavaFX FXML documents, but you cannot use Nashorn as the JavaScript engine within WebView.
Background Source Information
There is some information on the JavaScript implementation in WebView provided by the JavaFX developers on the JavaFX mailing list. Quoting Richard Bair, an Oracle JavaFX developer (who in turn quotes Olivier Hunt, a WebKit Developer):
Well…. it goes like this. WebKit comes with JavaScriptCore by default, and this is the JS engine that we use. It is pretty good. My understanding is that JavaScriptCore (aka SquirrelFish aka Nitro) is the same JS engine used by Safari. When Chrome and Apple were both part of WebKit, one of the abstraction layers that Google had put into WebKit was the ability to swap out the JavaScript engine. When Google forked WebKit into Blink, the need for the WebKit project to have an abstraction for a different JS VM disappeared. As a consequence, the WebKit guys have been talking about removing those abstractions such that you won't be able to swap out the JS engine, [further info]. If/When that happens, it will be hard (or impossible) for us to switch over to Nashorn for WebView. We aren't going to fork WebKit, so we sort of have to follow along with what WebKit does.
"Supporting V8 places a considerable burden on webkit, there are a number of
large, cumbersome and expensive abstractions required for to support multiple
JS engines (see the original discussions on the topic from many years ago).
Additionally we will only be supporting JSC in WebKit2, so I don't think anything could
convince me at least that maintaining support for multiple JS engines is good for the project." - Oliver Hunt
Disclaimer
This answer is related to Oracle Java 8 releases only; alternate and future JavaFX and Java implementations may have different internal implementations.
May it be used separately from WebView, just as JSR 223 engine?
JavaScriptCore in WebView cannot be used separately from WebView as a JSR 223 engine (as far as I know).
I think it is best to treat the JavaScriptCore implementation shipped to support WebView as an internal implementation detail of the WebView component and not treat the JavaScript implementation as a general purpose JavaScript Runtime to be used elsewhere. It is not an officially supported component outside of its specific use within WebView, and, when used there, the public WebView API shields you from the implementation details of the JavaScript runtime it uses: Such that it is deliberately opaque to users which JavaScript runtime is actually used to execute JavaScript within the WebView component.
Update
Since OP is more concerned about JavaFX WebView, it uses JavaScriptCore. Please read JewelSea's answer for a better understanding.
JavaFX Webkit does not use Nashorn, it uses JavaScriptCore
Oracle Nashorn is the JavaScript engine in Java 8. Until Java 7, the JavaScript engine used was based on Mozilla Rhino.
Yes, it is same for Oracle JDK and OpenJDK.
You can find more information on Nashorn on the Oracle Site.
I have a main application that is coded with Java, but I want to use Python because the NLTK lib.
I want to know if it's possible do some integration with these two languages. Thinks like passing parameters from Java to Python and retrieving processed data from Python to Java.
Try Jython
From their FAQ site:
What is Jython?
Jython is the successor to JPython. The Jython project was created in
accordance with the CNRI JPython 1.1.x license, in order to ensure the
continued existence and development of this important piece of Python
software. The intent is to manage this project with the same open
policies that are serving CPython so well.
What is JPython?
JPython is an implementation of the Python programming language which
is designed to run on the Java(tm) Platform.
Recently some of my users have been reporting problems with NullPointers. Thanks to one of them, I have managed to find out that my application can't find the Javascript engine that should theoretically come with the JRE (most of them have the JDK anyway).
How can they manually install the scripting engine?
Thanks in advance...
P.S. Most of these people have OpenJDK. However, this doesn't happen to me (I am also an OpenJDK user)...
EDIT: They have at least version 1.5, most of them have 1.6.
From JSR 270 (the Java 6 SE spec):
There will be no requirement that any
particular scripting language be
supported by the platform;
implementors may choose to include
support for the scripting language(s)
of their choice as they see fit.
So, it is conceivable that there are JREs out there without JavaScript support. There may also be variations in engine names, language names and versions bundled. "JavaScript" is an Oracle trademark so some vendors might be reluctant to use it as an identifier string.
The Rhino engine has only been bundled with Java since Java 6. Thus, your Java 5/1.5 users won't have it preinstalled.
That said, you can get Rhino, the Javascript engine, manually from Mozilla's site, since the engine itself does work with Java 5. However, you will have to use Rhino's API, rather than working with Java's ScriptEngine interface.
I need to use the logic contained in some java classes. I found JNI, but that project seems not updated recently.
Is there a way to use it in a Delphi native application? I use Delphi 2009.
A newer solution than JNI is JNA, which also supports callbacks from (Delphi) DLLs. I found it easy to use.
You could try j-interop.
The technique I should adopt is to build a COM wrapper of java business logic, and using this COM server from delphi throught interoperability.
Delphi can build a type library of a COM server, and you could istantiate the com server using this typelibrary. Type library is simply a wrapper of the server, exposing its interface to be used by delphi code.
The key to communicating with different platform softwares is called "interoperability".
You can find this also in .net versus win32. Tipically delphi code is win32 (exe or dll), and you can build communication protocol between delphi objects and .net assemblies or java bytecode using interoperability solutions.
With Java 6, it takes only a few lines of code to write a standalone web service server which then can be invoked from Delphi.
small step-by-step tutorial, using the free NetBeans IDE and Delphi:
Delphi and Java Integration using Web Services
more xamples:
http://www.theserverside.de/webservice-in-java/
http://www.torsten-horn.de/techdocs/jee-jax-ws.htm#Minimaler-Webservice
Embed the VM in the native code. This worked for me.
An example with c can be found here. http://java.sun.com/docs/books/jni/html/invoke.html
I very much doubt it. Delphi Code gets compiled into an executable while Java code is executed by a Virtual Machine. So unless you launch a complete Virtual Machine inside Delphi code I see no way to easily include the logics.