0

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
9
  • Rhino compiles js to class, you could Provide an Applet runtime (for security) and run the compiled Java code directly. Commented Jun 5, 2015 at 7:09
  • I just updated my question with more information about my use-case. I need to run the this on a server, not in a browser. Unless you know something I don't, it's not any easier to run an applet in a server than to dynamically load a jar and run it. Commented Jun 5, 2015 at 15:35
  • Why don't you just write JavaScript? Commented Jun 5, 2015 at 15:36
  • We're an all Java shop. We have large apps and the organization has purposely chosen Java because of the structure that static typing provides. I'm considering an alternate deployment strategy for our existing apps. If I were to suggest that we rewrite all our apps in JavaScript, that conversation would not go well =). Commented Jun 5, 2015 at 15:50
  • Have you had a look at osgi stacks? Like equinox or apache felix? Otherwise I think the idea to use gwt that way seems to work for you - you should possibly answer the question, or rephrase it, as I see no question to be answered anymore :-) Commented Jun 6, 2015 at 13:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.