如何将Java源代码交叉编译为JavaScript?

发布于 2024-07-11 13:39:15 字数 1258 浏览 5 评论 0原文

给定一组 Java 源代码文件,如何将它们编译成一个或多个可以与手工编写的 JavaScript 一起使用的 JavaScript 文件?

GWT 是一种选择,但到目前为止我看到的每个示例都旨在构建精美的网站。 将 Java 源代码转换为 Javascript 的简单用例,可以与手工编写的 JavaScript 一起使用 尚未有详细记录

我在GWT 邮件列表上发起了一个帖子关于这个问题,但对于这是否可行,意见似乎不一。

有人给出了一个非常有用的提示,那就是查看 GWT-Exporter。 问题是源代码和文档都不容易获得,尽管有 这个这个

编辑:GWT-Exporter源代码在这里

我也看到了Java2Script。 但同样,我无法找到如何解决我的简单用例的示例。

解决这个问题的最佳方法是什么? 我还缺少什么更好的东西吗?

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 every example I've seen so far is aimed at building fancy websites. The simple use case of just converting Java source to Javascript that can be used together with handcrafted JavaScript hasn't been well-documented.

I started a thread on the GWT mailing list on this subject, but opinions seem to be mixed on whether this is even feasible.

One person gave a very useful tip, which was to check out GWT-Exporter. The problem is that neither source code nor documentation is readily available, although there's this and this.

edit: GWT-Exporter source code is here

I've also seen Java2Script. But again, I wasn't able to find examples of how to solve my simple use case.

What's the best approach to this problem? Is there something better I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

彼岸花ソ最美的依靠 2024-07-18 13:39:15

当您使用 GWT 时,您基本上是将 UI 部分转换为 Javascript(并且假设您使用编写 Java 时提供的 UI 小部件)。 只有部分 Java 库可以在 Javascript 中访问。 通常,在 GWT 应用程序中,大量使用 Java 库的任何内容都将在服务器端运行,并以 AJAX 的形式连接到 Javascript(GWT 会为您处理)。 因此,GWT 不一定会将您的完整应用程序转换为 Javascript(尽管如果您愿意限制对 Java 库和某些功能的使用,则可以)。

无论如何,如果这种方法(从 Javascript 中调用在服务器上运行的 Java)对您有吸引力,那么一个不错的选择是 DWR< /a>,它基本上允许您的 Javascript 直接调用服务器上运行的 Java 类中的方法(无需构建 Web 服务或其他前端)。 我知道不是你问的。

更相关的是,看起来有 演示 gwt-exporter 用法的示例应用

When you use GWT, you're basically converting the UI portion into Javascript (and it assumes that you use the UI widgets provided when you write your Java). Only some of the Java libraries are accessible within Javascript. Typically in a GWT application anything that makes heavy use of Java libraries would run on the server side and connect to the Javascript as AJAX (which GWT handles for you). So GWT isn't necessarily converting your full application into Javascript (though it can if you're willing to limit your use of Java libraries and some functionality).

At any rate, if this approach (calling out to Java running on a server from within Javascript) appeals to you, one nice option is DWR, which basically allows your Javascript to directly call methods in Java classes running on the server (without you having to build a web service or other frontend). Not what you asked, I know.

More relevantly, it looks like there's source code for a sample app demonstrating the use of gwt-exporter.

身边 2024-07-18 13:39:15

我不确定它是否适合您的用例,但如果您同意放弃 Java API 并使用 Java 中的 JavaScript API,那么您可以使用 JSweet,一个构建在 TypeScript 之上的 Java 到 JavaScript 转译器。 它使您可以访问数百个类型良好的 JavaScript API(DOM、jQuery、underscore、angularjs 等)。 它生成 JavaScript 代码,您可以将其与旧版 JavaScript 和 TypeScript 代码混合。

注意:JSweet 不适用于旧版 Java 代码和旧版 Java API,但您的用例没有提到重用旧版代码。

[更新] 从 1.1 版开始,JSweet 现在还支持一些 Java API,例如 Collections (java.util)。 因此,在一定程度上重用遗留Java代码是可能的。 添加您自己的 Java API 支持也非常简单。

I am not sure if it fits your use case, but if you agree to drop Java APIs and use JavaScript APIs from Java, then you can use JSweet, a Java to JavaScript transpiler built on the top of TypeScript. It 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 for legacy Java code and legacy Java APIs, but your use case did not mention reusing legacy code.

[UPDATE] Since version 1.1, JSweet now also supports some Java APIs such as Collections (java.util). So, it is possible to reuse legacy Java code to a certain extent. It is also quite straightforward to add your own support for Java APIs.

寻找一个思念的角度 2024-07-18 13:39:15

给定一组 Java 源代码文件,如何将它们编译成一个或多个可与手工编写的 JavaScript 一起使用的 JavaScript 文件?

尽管有很多 转换 Java 的解决方案应用程序到 Javascript,您对新的 javascript 代码可以与结果代码交互的解决方案感兴趣。 这是其他答案的更新(截至 2018 年)。

有不同类型的工具。 例如,您可能会找到允许您 (1) 将 java 代码转换为 javascript 的工具; (2) 将字节码转换为javascript、asm.js或webAssembly; (3) 直接在浏览器中执行 java 应用程序,以及 (4) 创建结合 java 和 javascript 的解决方案。 您必须根据您的要求选择要使用的解决方案。


将 Java 源代码转换为 Javascript

一些解决方案采用 Java 源代码并生成 JavaScript 等效版本。 通常,这些解决方案将 Java 转换为 Javascript,但不支持 Java 运行时的所有行为和库。 生成的代码可能不支持某些 java 标准库。 通常,它们用于使用 Java 创建 HTML 应用程序,但不用于迁移代码。
优点:生成的解决方案可能包含非常小的文件。 您可以使用它来重用自己的业务逻辑类,而无需考虑 GUI 或特定于平台的库。
缺点:您可能无法使用 Java 平台的某些功能。 它需要访问源代码。

  • JSweet 将 Java 转换为 javascript。 它包括 1000 多个 JavaScript 库的 API 绑定。 您可以编写使用这些库的 java 代码。
  • j2s,是 Eclipse RAP 平台用来将 java 代码转换为 javascript 的编译器。 它用于将 SWT (GUI) 小部件转换为 javascript 和 HTML。 它不支持所有Java标准库



将 Java 字节码转换为 Javascript

这些解决方案采用编译后的 java 代码(.class 文件)并生成 javascript、asm.js 或 webassemble 中的等效代码。 考虑到java代码可能依赖于java标准库(即JRE),这些解决方案通常包括移植和预编译的库。
优点:您无需更改代码中的任何内容。 您也不需要源代码。
缺点:最终的解决方案可能需要加载大量文件。

  • Bck2Brwsr,一个可以提前将 java 字节码编译为 javascript 的 Java VM。 它为每个 .jar 文件生成一个 javascript 文件。
    • 您可以使用 vm javascript 对象将类加载到 javascript 中并执行静态方法(使用 vm.loadClass(.., function(class){..}})的文档中有一个 gradle 插件< /a> 和 maven 任务


  • 。 //teavm.org/" rel="nofollow noreferrer">TeaVM,是另一个 Java VM,可以提前将代码转换为 javascript。与 Bck2Brwsr 相比,它支持线程,生成单个文件为您的所有类提供更好的调试支持。
  • DukeScript,使用 Bck2Brwsr 或 TeaVM 将 java 代码和字节码转换为 javascript。


  • Dragome,将java字节码转译为javascript。
  • CheerpJ (商业产品)可以使用 Swing 和 AWT 运行完整的 java 应用程序。 它提供了一个非常完整的javascript环境,支持操作系统、线程和网络功能。
    • 它提供了完整的运行时 API。 您可以使用 cheerpjRunMain(,) 运行 main 方法。 您可以使用 cjNew(,...) 创建对象并使用 cjCall(,,调用静态方法;...)。 您还可以考虑许多其他方法。

在 Javascript 中运行 Java 代码

DoppioJVM 是一个用 Typescript 编写的完整 JVM。
优点:它模拟操作系统的许多元素,包括文件系统、TTY 控制台和线程。
缺点:考虑到它是一个解释器,它的结果可能比其他解决方案慢。 (我没有测试过)

  • DoppioJVM 是一个用 Typescript 编写的 JVM
    • 文档包含用于加载和运行类。 您可以使用 jvm.runClass(, [...], function(response){..}) 运行静态方法。 您可以运行 Jar 文件并执行许多其他任务。

创建结合 Java 和 Javascript 的应用程序

一些其他解决方案不仅提供编译代码的工具,还提供创建 java 和 javascript 解决方案的框架和解决方案。 例如,CheerpJ 具有用于图形用户界面的完整版本的 Swing 和 AWT 库,但它们可能会非常慢。 您可以使用在浏览器上运行速度更快的新 HTML 版本来替换用户界面。
优点:您可以重用现有代码而无需更改,主要是一些库和业务逻辑。 您可以从解决方案中删除在浏览器中运行效率较低的库。
缺点:如果你想继续维护你的java桌面版本,你必须为浏览器处理不同的代码。


推荐

  • 如果你想复用一些自己创建的类,你可以尝试 JSweet 。 您可以创建 javascript 模块(库),您可以轻松地将其与 javascript 和 typescript 一起使用。
  • 如果您想重用依赖多个 java 库的中型到大型代码库,您可以尝试 CheerpJDukescript
  • 如果你想在不做任何改变的情况下运行完整的java应用程序,你可以尝试CheerpJ。 它可以运行 Swing 和 AWT 用户界面。 它还提供 Applet 运行程序

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.
  • 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

Converting Java 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.
  • DukeScript, transpile java code and bytecode to javascript using Bck2Brwsr or TeaVM.
  • Dragome, transpile java bytecode to javascript.
  • 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( <class>, <jar> ). You can create objects using cjNew( <class>, <params>...) and invoke static methods using cjCall( <class>,<method>,<params>...). 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( <class>, [ <args>...], 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.


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.
嗫嚅 2024-07-18 13:39:15

虽然问题是关于将 Java 源代码编译为 JavaScript,但我认为值得一提的是 TeaVM 将 Java 字节码编译为 JavaScript。 我从未尝试过,但看起来很有希望。

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

一笑百媚生 2024-07-18 13:39:15

这里还有另外两个选择,需要研究的事情,第三个选择是不转换,只是住在一起。

  1. Java2Javascript

我一直想尝试一下——看起来更接近所要求的内容。 引用网页:

一个 Eclipse Java 到 JavaScript 编译器插件以及 JavaScript 版本的 Eclipse Standard Widget Toolkit (SWT) 的实现以及其他常见实用程序,例如 java.lang.* 和 java.util.*。 您可以通过 Java2Script Pacemaker 将基于 SWT 的富客户端平台 (RCP) 转换为富互联网应用程序 (RIA)。

Javascript 中的 Java 经验有限 - 您需要移植必要的依赖项或通过 jQuery 等工具找到替代方案。

  1. DukeScript

在我看来,DukeScript 会编译一些前端 Javascript,并从浏览器的 Javascript 调用后面的 Java。 这似乎或多或少是一种混合方法,因此您可以使用 Javascript 中的 Java 丰富的库。 我将违反 Java 的浏览器安全策略。

利用 Java 运行时提供更完整的 Java Javascript 体验。 如果我想在浏览器环境之外实现这一点,我会在 Java 上使用 Javascript。

  1. Nashhorn

将此视为一个示例使用 Java 的资源作为 Javascript 的基础: Nashorn 和 JavaFX,作为富 Javascript 操作客户端的示例。 无论如何,使用 Java 内部的 Javascript 引擎,您不需要在 Javascript-VM 到对象代码再到 Java-VM 之间进行太多转换。

Here's two other options, things to look into and a third option not converting, just living together.

  1. Java2Javascript

I have been wanting to try this out -- Looks closer to what's been asked. Quoting the web page:

an Eclipse Java to JavaScript compiler plugin and an implementation of JavaScript version of Eclipse Standard Widget Toolkit (SWT) with other common utilities, such as java.lang.* and java.util.*. You can convert your SWT-base Rich Client Platform (RCP) into Rich Internet Application (RIA) by Java2Script Pacemaker.

A limited Java in Javascript experience - You'd need to port your necessary dependencies or find alternatives via tools like jQuery, etc.

  1. DukeScript

As I view DukeScript, it compiles some front-end Javascript and calls Java behind, from the browser's Javascript. It seems more or less a hybrid approach so you can use the Java wealth of libraries from Javascript. I will fall foul of a browser security policy for Java.

A more full-Javascript on Java experience leveraging the Java-runtime. If I wanted that outside the browser environment I'd use Javascript on Java.

  1. Nashhorn

Consider this as an example of using Java's resources as foundation for Javascript: Nashorn and JavaFX, as an example for a rich Javascript operated client. Anyway with a Javascript engine inside Java you're not needing to translate between a Javascript-VM to object-code to a Java-VM quite so much.

输什么也不输骨气 2024-07-18 13:39:15

不完全切题,但 Kotlin 是一种 100% Java 兼容的语言,可以编译为 JavaScript。

IntelliJ IDEA 可以自动将 Java 转换为 Kotlin 并编译以在 Node 或浏览器上运行。

Not entirely on-topic, but Kotlin is a 100% Java-compatible language that can compile to JavaScript.

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

叹梦 2024-07-18 13:39:15

给定一组Java源代码文件,如何将它们编译成一个
或更多可与手工制作一起使用的 JavaScript 文件
JavaScript?

内置 Java API 和 Java 语言功能与 JavaScript 之间没有直接关联。 因此,任何创建“转换器”的尝试都将是不完整的。 作为一个基本示例,Java 类没有直接对应的 JavaScript 习惯用法。

如果没有源代码,就不可能知道不完整的转换工具是否适合您的用例。


也就是说,我解决您问题的建议是首先尝试使用 GWT:设置一个演示项目,放入库的源代码并从 JavaScript 端调用它,然后查看 GWT 在其 .js 文件中输出什么。 其他发帖者建议的其他一些工具也绝对值得一试。

如果这是富有成效的并且让你有所作为,那就太好了。

从那里,您将需要/想要手动完成剩余的转换。 毕竟,假设您希望代码实际正确运行,那么手动检查肯定是合适的。 一些单元测试随之转换也是理想的。

您没有说明项目的源代码有多大,但如果它很小(假设少于一千行代码),即使手动完成转换也应该不会非常困难。 如果它比这个大得多,我建议您检查一下您是否确实希望将其作为 JavaScript 代码。

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?

There is no direct correlation between both the built-in Java API and Java language features, and those of JavaScript. So any attempt at creating a "converter" is going to be incomplete. As a fundamental example, Java classes don't have a direct corresponding JavaScript idiom.

Whether or not an incomplete conversion tool will work for your use case is impossible to know without the source code.


That said, my suggestion to solving your problem would be to first attempt to use GWT: set up a demo project, drop in the source of your library and call it from the JavaScript side and see what GWT outputs in it's .js file. Some of the other tools suggested by other posters here are definitely worth checking out as well.

If that is fruitful and gets you part of the way, great.

From there, you'll need/want to do the remainder of the conversion by hand. After all, assuming you want the code to actually function correctly, a manual review would definitely be in order. Some unit tests being converted along with it would be ideal as well.

You don't state how large the source of your project is, but if it's small (let's say less than a thousand lines of code), even a complete conversion by hand shouldn't be extremely difficult. If it's much larger than that, I would suggest reviewing if you actually want that as JavaScript code anyway.

杀お生予夺 2024-07-18 13:39:15

这个问题是13年前的; 是的,我知道。 但我将在这里添加我的补充,因为这是关于该主题的优秀答案存储库,并且由于它在 Google 搜索中首先出现,因此可能会吸引最多的眼球。

我是一名前 GWT 开发人员。 并同意这是一个很好的解决方案& 技术。 但正如前面提到的,交叉编译部分主要(可以说是完全)集中在其名称中的 W(Google WEB Toolkit):全栈 SPA Web 应用程序的客户端 JavaScript 部分、其序列化和序列化。 通信,客户端/服务器逻辑分区等。

我只是偶然发现了一些相对较新的东西——GWT 的 2014-2015 年分支于 2018 年被置于公共领域——我认为它更普遍地集中在原始帖子中所要求的内容(13 年前,哈哈) 。 我很想尝试一下。

J2CL:https://github.com/google/j2cl

我在这里读到了它,这对我来说是一个很好的& 该技术的彻底介绍(看看如果您同意或不同意请告诉我):

https://www.infoq.com/news/2019/05/j2cl-java-javascript-transpiler/

This question is from 13 years ago; yes, I know. But I'll put my addition here as this is an excellent repository of answers on the topic, and as it comes up first in a Google search it is likely to have the most eyeballs on it.

I'm a former GWT developer. And agree it is an excellent solution & technology. But also yes as mentioned, the cross compilation part mostly (arguably, completely) focuses on the W in its name (Google WEB Toolkit): the client-side JavaScript part of full stack SPA Web applications, its serialization & communication, it's client/server logic partitioning, et al.

I just stumbled onto something that is relatively new -- a 2014-2015 offshoot of GWT placed into the public domain in 2018 -- that I think is more generally focused and what was being requested in the original post (13 years ago, lol). I'm eager to try it.

J2CL: https://github.com/google/j2cl

I read about it here, which to me was a good & thorough intro of the technology (have a look and let me know if you agree or not):

https://www.infoq.com/news/2019/05/j2cl-java-javascript-transpiler/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文