Oracle recently released Java 8, which includes Oracle Nashorn as a JavaScript engine. Does Nashorn only works one way in that you can convert JavaScript code into Java bytecode? Or is there any way to use it to convert Java code into JavaScript code?
-
1The only thing that I know to convert/compile java to javascript is Google Web Toolkit (GWT)Alkis Kalogeris– Alkis Kalogeris2015-02-27 16:54:51 +00:00Commented Feb 27, 2015 at 16:54
-
I don't think Nashorn generates any bytecode -- it's method handles all the way down.Jeffrey Bosboom– Jeffrey Bosboom2015-03-08 20:08:14 +00:00Commented Mar 8, 2015 at 20:08
-
Can you check if my answer is correct?Ortomala Lokni– Ortomala Lokni2015-06-05 15:20:29 +00:00Commented Jun 5, 2015 at 15:20
-
1This question asks how to convert java to js and it has some interesing answers. stackoverflow.com/questions/440302/…aalku– aalku2015-06-08 06:50:51 +00:00Commented Jun 8, 2015 at 6:50
1 Answer
The official page of the Project Nashorn says:
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.
So it's not the goal of the Nashorn projet to convert Java code to Javascript.
You can use instead the GWT compiler wich does that.
The GWT SDK provides a set of core Java APIs and Widgets. These allow you to write AJAX applications in Java and then compile the source to highly optimized JavaScript that runs across all browsers, including mobile browsers for Android and the iPhone.
Beware that only a part of the Java Runtime Library can be emulated, you can find the list under this link.
gwt-exporter is also a useful tool for your purpose.
Develop an application or library in GWT and use gwtexporter annotations to make classes and methods available from javascript.