我是GWT的巨大粉丝(将Java转换为在浏览器中运行的JavaScript),但是有没有将Java转换为JavaScript以在JVM中运行的任何工具通过Nashorn或Rhino)?将Java转换为在JVM上运行的JavaScript
更新
更多信息...我探索的想法是编译后端代码为JavaScript,然后将其存储在数据库中。然后,Web应用程序可以将后端“模块”拉出数据库并在服务器上执行它。
更新
我没有张贴此作为一个答案,因为我怀疑这是一个好主意,但出于好奇我试图在JVM上运行GWT生成的JavaScript。在“详细”级别编译时,我不得不模拟3个GWT期望存在的DOM对象(hack!),但它确实有效。
Test.gwt.xml
<module rename-to='test'>
<entry-point class='test.client.Test'/>
<source path='client'/>
<source path='shared'/>
<super-source path="jre"></super-source>
<add-linker name="xsiframe"/>
</module>
...注意超源JRE ...
测试/客户/ Test.java
public class Test implements EntryPoint {
public void onModuleLoad() {
print("" + new File("/Users/.../someFile.txt").exists());
print("Hey");
}
public native void print(String msg) /*-{
print(msg);
}-*/;
}
...的嘲笑文件类...
test/jre/java/io/File.java
public class File {
public File(String name) {
_init(name);
}
private native void _init(String name) /*-{
this._obj = new java.io.File(name);
}-*/;
public native boolean exists() /*-{
return this._obj.exists();
}-*/;
}
测试/ RhinoTest.java
public class RhinoTest {
public static void main(String[] args) throws IOException, ScriptException {
String js = FileUtils.readFileToString(new File("/Users/.../Test/war/test/130CD4F977EDEB096DFEF9871580F1CD.cache.js"));
js = "var $wnd = { test: { __sendStats: function() {} } };"
+ js;
new ScriptEngineManager().getEngineByName("JavaScript").eval(js);
}
}
产地:
trueHey
Rhino将js编译为类,您可以提供Applet运行时(用于安全性)并直接运行编译后的Java代码。 – thst
我刚刚更新了我的问题,提供了有关我的用例的更多信息。我需要在服务器上运行这个,而不是在浏览器中运行。除非你知道我没有的东西,否则在服务器上运行applet并不比在动态加载jar并运行它更容易。 – Stinky
你为什么不写JavaScript? –