2

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?

4
  • 1
    The only thing that I know to convert/compile java to javascript is Google Web Toolkit (GWT) Commented Feb 27, 2015 at 16:54
  • I don't think Nashorn generates any bytecode -- it's method handles all the way down. Commented Mar 8, 2015 at 20:08
  • Can you check if my answer is correct? Commented Jun 5, 2015 at 15:20
  • 1
    This question asks how to convert java to js and it has some interesing answers. stackoverflow.com/questions/440302/… Commented Jun 8, 2015 at 6:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

As far as I know GWT toolchain detects what java code is needed on client-side and convert to js only those snippets.
I've modifed my answer to take your remark into account.
sweeeeet great answer ya'll!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.