How to Cross-Compile Java Source Code to JavaScript?

后端 未结 7 1474
醉酒成梦
醉酒成梦 2020-12-12 19:02

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

GWT is one option, but

7条回答
  •  执笔经年
    2020-12-12 19:45

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

    Although there are many solutions to convert Java applications to Javascript, you are interested on a solution where new javascript code may interact with the resulting code. This is an update (as 2018) of the other answers.

    There are different types of tools. For instance, you may find tools that allow you (1) convert java code to javascript; (2) convert bytecode to javascript, asm.js or webassembly; (3) execute java applications directly in the browser and (4) create solutions that combine java and javascript. You must select the solution to use depending on your requirements.


    Converting Java source code to Javascript

    Some solutions take java source code and produce a javascript equivalent version. Usually, these solutions transforms the Java to Javascript, but do not support all the behaviours and libraries of the Java runtime. The resulting code may not support some java standard libraries. Typically, they are used to create HTML application using Java but not for migrating the code. Pros: The resulting solution may include very small files. You can use it to reuse your own business logic classes without considering GUI or platform specific libraries. Cons: it is possible that you cannot use some functionalities of the Java platform. It requires access to the source code.

    • JSweet converts Java to javascript. It includes API bindings for 1000+ javascript libraries. You can write java code that use these libraries.
      • It converts java to typescript and later uses the typescript compiler to create the corresponding javascript.
      • It can produce multiple types of javascript modules and typescript definitions. You can use the resulting code in javascript very easily.
    • j2s, is the compiler used by the Eclipse RAP platform to translate java code to javascript. It is used there to convert the SWT (GUI) widgets to javascript and HTML. It does not support all the Java standard libraries
      • It may convert simple Java classes to be used in javascript. You must use the classes considering the OO Java-Javascript simulation performed by j2s.

    Converting Javascript bytecode to javascript

    These solutions take compiled java code (.class files) and produces equivalent code in javascript, asm.js or webassembly. Considering that the java code may depend on java standard libraries (i.e., the JRE), these solutions typically includes ported and pre-compiled libraries. Pros: you do not need to change anything in your code. You do not need the source code neither. Cons: the resulting solution may require the load of a lot of files.

    • Bck2Brwsr, a Java VM that may compile ahead-of-time the java bytecode to javascript. It produces a javascript file for each .jar file.
      • You may use the vm javascript object to load a class into javascript and execute static methods (using vm.loadClass(.., function(class){..}}). There is an example in the documentation for the gradle plugin and the maven task.
    • TeaVM, is another Java VM that may convert ahead-of-time the code to javascript. In contrast to Bck2Brwsr, it supports threads, produces a single file for all your classes and provide better debugging support.
      • You may call java from javascript using the javaMethods.get(..).invoke(...) method.
    • DukeScript, transpile java code and bytecode to javascript using Bck2Brwsr or TeaVM.
      • It provides maven tasks to compile the java code. The resulting code (and technique to call java from javascript) depends on the used transpiler.
    • Dragome, transpile java bytecode to javascript.
      • You can annotate static methods in Java and use it in Javascript: e.g. you can annotate a method with @MethodAlias(alias="windows.method1") and invoke it from javascript using window.method1()
    • CheerpJ (a commercial product) may run complete java applications using Swing and AWT. It provides a very complete javascript environment that support operating system, thread and network functionalities.
      • It provides a complete runtime API. You can run a main method using cheerpjRunMain( , ). You can create objects using cjNew( , ...) and invoke static methods using cjCall( ,,...). There are many other methods you may consider.

    Running Java code in Javascript

    DoppioJVM is a complete JVM written in Typescript. Pros: It emulates a lot of elements of the operating system, including filesystems, TTY consoles and threads. Cons: Considering that it is an interpeter, it may result slower than other solutions. (I have not tested it)

    • DoppioJVM is a JVM written in Typescript
      • The documentation includes snippets of code to load and run the classes. You can run a static method using jvm.runClass( , [ ...], function(response){..}). You can run a Jar file and perform many other tasks.

    Create applications combining Java and Javascript

    Some other solutions provide, not only the tools for compiling the code, but also frameworks and solutions to create java and javascript solutions. For instance, CheerpJ has complete versions of the Swing and AWT libraries for graphical user interfaces, but they may result very slow. You may replace the user interface by using new HTML versions that run faster on the browser. Pros: You can reuse existing code without changes, mainly some libraries and business logic. You may remove from your solutions libraries that run not efficiently in the browser. Cons: If you wanna keep maintaining your java desktop version, you must deal with different code for the browser.

    • GWT converts Java code to javascript but uses a different set of libraries for the user interface and client-to-server communications.
      • The documentation maintains information of differences with the JRE
      • You may export Java classes to be used by Javascript code.
    • Dukescript uses the conversion tools mentioned before.
      • It provides a Knockout4j library that may interact easily with HTM, DOM and additional javascript code.
    • Dragome not only convert java code but also include means to interact with the HTML/DOM
      • You may use annotations to bind java objects to HTML elements and to handle the DOM
    • HTML/Java API is an Apache project that standardize the access to HTML/DOM/javascript from transpiled code
      • It was donated by Dukescript and other tool providers.

    Recommendation

    • If you wanna reuse few classes created by your own, you may try JSweet. You may create javascript modules (libraries) that you can use easily with javascript and typescript.
    • If you wanna reuse a medium to large codebase that rely on multiple java libraries, you may try CheerpJ, Dukescript or Dragome. You may reuse large parts of your code and create (gradually) the user interface and client-to-server communications using technologies that are more browser-friendly.
    • If you wanna run complete java applications without change, you may try CheerpJ. It can run Swing and AWT user interfaces. It also provide an Applet runner.

提交回复
热议问题