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 Feb 27 '15 at 16:54
-
I don't think Nashorn generates any bytecode -- it's method handles all the way down. – Jeffrey Bosboom Mar 08 '15 at 20:08
-
Can you check if my answer is correct? – Ortomala Lokni Jun 05 '15 at 15:20
-
1This question asks how to convert java to js and it has some interesing answers. http://stackoverflow.com/questions/440302/how-to-cross-compile-java-source-code-to-javascript – aalku Jun 08 '15 at 06:50
1 Answers
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.

- 56,620
- 24
- 188
- 240
-
As far as I know GWT toolchain detects what java code is needed on client-side and convert to js only those snippets. – aalku Jun 03 '15 at 14:16
-
-