如何将Java源代码交叉编译为JavaScript? [英] How to Cross-Compile Java Source Code to JavaScript?

查看:429
本文介绍了如何将Java源代码交叉编译为JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一组Java源代码文件,如何将它们编译成一个或多个可以与手工制作的JavaScript一起使用的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?

GWT 是一种选择,但到目前为止我看到的每个例子都是为了建立花哨的网站。将Java源代码转换为Javascript的简单用例,可以与手工编写的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在GWT邮件列表上启动帖子这个主题,但是对于这是否可行,看来似乎有些混淆。

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

一个人提供了一个非常有用的提示,即检查 GWT-Exporter 。问题是,无论是源代码还是文档都没有,尽管有这个这个

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.

编辑:GWT-Exporter源代码这里

edit: GWT-Exporter source code is here

我也见过 Java2Script 。但同样,我无法找到如何解决我的简单用例的例子。

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?

推荐答案

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

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

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

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.

更相关的是,它看起来像示例应用程序演示使用gwt-exporter

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

这篇关于如何将Java源代码交叉编译为JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆