How to cross Java source code with JavaScript?

Given the set of Java source code files, how can I compile them into one or more JavaScript files that can be used with manually created JavaScript?

GWT is one option, but every example I've seen so far is aimed at creating trendy websites. A simple version of a simple conversion of a Java source to Javascript that can be used with JavaScript with manual processing has not been well documented .

I started a topic on the GWT mailing list on this subject, but the opinions seem to be mixed about as much as possible.

One person gave some very useful advice that the GWT-Exporter should have checked. The problem is that neither source code nor documentation is available, although this and that are there .

edit: GWT-Exporter source code here

I also saw Java2Script . But then again, I could not find examples of how to solve my simple use case.

What is the best approach to this problem? Is there anything better I'm missing?

+46
java javascript code-translation
Jan 13 '09 at 19:01
source share
6 answers

When you use GWT, you basically convert part of the user interface to Javascript (and it is assumed that you use the user interface widgets provided when writing your Java). Only some of the Java libraries are available in Javascript. Typically, in a GWT application, anything that makes heavy use of Java libraries will run on the server side and connect to Javascript as AJAX (which GWT handles for you). Thus, GWT does not necessarily turn your complete application into Javascript (although it can, if you want to limit the use of Java libraries and some functions).

Anyway, if this approach (calling Java running on a server from Javascript) appeals to you, one good option is DWR , which basically allows your Javascript to directly call methods in Java classes running on the server (without having to create a web service or other interface). Not what you asked, I know.

Moreover, it seems that the source code for the sample application demonstrates the use of gwt-exportorter .

+9
Jan 13 '09 at 19:47
source share

I'm not sure if this fits your use case, but if you agree to give up the Java API and use the JavaScript API from Java, you can use JSweet , the Java transporter for JavaScript built at the top of TypeScript. This gives you access to hundreds of well-typed JavaScript APIs (DOM, jQuery, underscore, angularjs, etc.). It generates JavaScript code, and you can mix it with legacy JavaScript and TypeScript code.

Note. JSweet will not work with legacy Java code and legacy Java APIs, but reuse of legacy code was not mentioned in your use case.

[UPDATE] Starting with version 1.1, JSweet now also supports some Java APIs, such as Collections (java.util). Thus, legacy Java code can be reused to some extent. It is also quite simple to add your own Java API support.

+8
Dec 16 '15 at 9:57
source share

While it comes to compiling Java sources in JavaScript, I think it's worth mentioning that there is TeaVM that compiles Java bytecode in JavaScript. I have never tried, but it seems very promising.

+7
Nov 17 '15 at 17:14
source share

Here are two other options, things that you need to see, and the third option, not converting, just live together.

I want to try this - Looks closer to what was asked. Webpage citation:

The Java Eclipse Java compilation plugin for JavaScript and the implementation of the JavaScript version of the Eclipse Standard Widget Toolkit (SWT) with other common utilities such as java.lang. * and java.util. *. You can convert your SWT-based Rich Client Platform (RCP) to Rich Internet Application (RIA) using the Java2Script Pacemaker.

Limited Java in Javascript - you will need to transfer the necessary dependencies or find alternatives using tools like jQuery, etc.

  1. DukeScript

Since I am considering DukeScript, it compiles some external Javascript and calls Java behind, from the Javascript browser. This seems like a more or less hybrid approach, so you can use the wealth of Java libraries from Javascript. I would ruin the browser security policy for Java.

More complete Java Javascript using Java-runtime. If I wanted to use Javascript in Java outside of the browser environment.

  1. Nashhorn

Consider this as an example of using Java resources as the basis for Javascript: Nashorn and JavaFX , as an example for a rich Javascript client. In any case, with the Javascript engine inside Java, you don’t have to translate much between Javascript-VM and object code in Java-VM.

+3
Feb 13 '15 at 2:04
source share

Not completely off topic, but Kotlin is a 100% Java-compatible language that can compile JavaScript.

IntelliJ IDEA can automatically convert Java to Kotlin and compile it to run on Node or in a browser.

+3
03 Sep '16 at 12:31 on
source share

Given the set of Java source code files, how can I compile them into one or more JavaScript files that can be used with hand-held JavaScript tools?

Direct correlation between the built-in Java API and the Java language features, as well as JavaScript. Therefore, any attempt to create a "converter" will be incomplete. As a fundamental example, Java classes do not have a direct JavaScript idiom.

Regardless of whether the incomplete conversion tool works for your use case, it is impossible to find out without source code.




However, my suggestion to solve your problem would be to first try using GWT : create a demo project, source your library and call it from the JavaScript side and see what GWT outputs to it .js file. Some of the other tools offered by other posters here are also worth checking out.

If it is fruitful and makes you part of the way, great.

From there, you will need / want to complete the rest of the conversion manually. After all, if you want the code to actually function correctly, a manual check will definitely be fine. Some unit tests that would be converted with it would also be ideal.

You do not indicate how large the source of your project is, but if it is small (even less than a thousand lines of code), even a complete manual conversion should not be extremely complicated. If this were much more, I would suggest considering whether you really want this as JavaScript code.

+1
Oct 05 '13 at
source share



All Articles