phil-daniels
phil-daniels

Reputation: 656

Convert Java to JavaScript that runs on JVM

I'm a huge fan of GWT (converts Java into JavaScript that runs in a browser), but are there any tools out there that convert Java to JavaScript to be run in the JVM (via Nashorn or Rhino)?

Update

More information... the idea I'm exploring is to compile back-end code to JavaScript, then store it in a database. Web apps could then pull a back-end "module" out of the database and execute it on the server.

Update

I'm not posting this as an answer, because I'm doubting it's a good idea, but out of curiousity I tried running GWT-generated JavaScript in the JVM. Compiling at "detailed" level I had to mock 3 DOM objects that GWT expects to exist (hack!), but it does work.

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>

...note the super-source jre...

test/client/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);
  }-*/;
}

...the mocked File class...

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();
  }-*/;
}

test/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);
  }
}

Produces:

trueHey

Upvotes: 0

Views: 685

Answers (1)

Sergiu Gordienco
Sergiu Gordienco

Reputation: 175

Java and JavaScript are similar like Car and Carpet are similar.

You will not found an universal solution, event one without bugs on complex code.

I, apologize but the best and the fastest solution is manually, step-by-step, because you will spent too much time on debug and on manual adjustment.

Upvotes: 1

Related Questions