18

Is there a good way to convert JavaScript to Java?

I have a JavaScript file which needs to be converted to Java. It's a calendar component written in JavaScript.

Pang
  • 9,564
  • 146
  • 81
  • 122
user1231111
  • 397
  • 2
  • 6
  • 16
  • 1
    what you can do is put it in a HTML `webView` and overload the `shouldOverrideUrlLoading` function and control what happens when the calendar is clicked. look at [this question](http://stackoverflow.com/questions/4780899/intercept-and-override-http-requests-from-webview) for more details. – thepoosh Apr 29 '12 at 07:16
  • 2
    You can have more success [transpiling](http://jbueza.blogspot.com/2011_07_01_archive.html) Java to javascript. Angry Birds on browser is done this way – Michael Buen Apr 29 '12 at 07:17
  • you can't. No converter tool yet have been built for this. :) – Subhrajyoti Majumder Apr 29 '12 at 07:17
  • Besides, Java is becoming more and more a subset of javascript rather than the other way around. Especially the lacking of lambda/closure on Java. So there, feasibility-speaking you'll have a hard time converting javascript to Java (as javascript has lambda and dynamic in nature), but you can convert Java to javascript :-) – Michael Buen Apr 29 '12 at 07:18
  • What you want to is *port* an application form one language to another one. That's certainly possible, though not necessarily trivial. You have to go the code step by step and think about how convert the constructs of one language to the other one. – Felix Kling Apr 29 '12 at 07:48
  • 7
    awesome question. As usual i have no idea why this question is closed. I am looking for an answer exactly like this but... NOoooo SO says we can't discuss this heretical concept of converting JS to Java or vice versa. – Oliver Watkins Apr 06 '14 at 11:22
  • I think this is a more important question than people make out it to be. Porting between languages is usually a very time-consuming process all to really achieve the same thing just in another language. As JavaScript is interpreted(to an extent), wouldn't porting it to the language that interprets it(I'm assuming some C based language) be feasible? – Damien Golding Sep 06 '14 at 11:40

1 Answers1

9

No, you can't.

The similar names are unfortunate but JavaScript and Java are totally different languages.

From Wikipedia:

JavaScript uses syntax influenced by that of C. JavaScript copies many names and naming conventions from Java, but the two languages are otherwise unrelated and have very different semantics. The key design principles within JavaScript are taken from the Self and Scheme programming languages.

More precisely: of course you can. But that involves writing or incorporating a JS engine, binary code, or an interpretor of some sort in your java program. A complete transpiler is probably possible but wouldn't be easy, especially in this direction, and of course hasn't been made.

Suma
  • 33,181
  • 16
  • 123
  • 191
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 17
    of course you could, if the javascript code is written in an OO way, then there is no reason why a converter couldnt convert it to java. JS handles inheritance a bit differently, and has no classes which would make it a challenge.. You would still get a bunch of compile problems probably. But I'd say it is NOT impossible. – Oliver Watkins Apr 06 '14 at 11:21
  • 1
    Good luck with that. The semantics of functions being objects and classes being hash maps in JS cannot be represented in Java, at least not in any usable form. – rustyx Jul 22 '15 at 09:32
  • Both are turing complete language, so why not? – Muhammad Umer May 26 '16 at 02:57
  • 2
    More recently, I [wrote a translator](http://stackoverflow.com/a/39551640/975097) that converts a small subset of the JavaScript programming language into Java. – Anderson Green Sep 17 '16 at 20:56