Article Discussion
Compiling Java to JavaScript
Summary: A key feature of the Google Web Toolkit (GWT) is that it allows developers to write browser-hosted Web applications entirely in Java, and have GWT translate that Java code into browser-specific JavaScript. In this interview with Artima, Scott Blum, the Google software engineer behind GWT's Java-to-JavaScript compiler, talks about the challenges of turning Java source code into JavaScript.
8 posts.
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: March 8, 2007 8:32 PM by Badri
    Frank
     
    Posts: 135 / Nickname: fsommers / Registered: January 19, 2002 7:24 AM
    Compiling Java to JavaScript
    December 21, 2006 5:30 PM      
    In this interview, Scott Blum said that writing Web applications in Java, and then automatically translating Java source code to JavaScript, lets you take advantage of Java developer tools and libraries. At the same time, he also said that JavaScript is a powerful language. What do you think of Blum's comments about JavaScript?

    http://www.artima.com/lejava/articles/java_to_javascript.html

    What do you think of this approach to creating JavaScript?
    • Leo
       
      Posts: 14 / Nickname: aeoo / Registered: April 12, 2006 7:02 AM
      Re: Compiling Java to JavaScript
      December 21, 2006 7:56 PM      
      One more note --

      I believe that the only time code generation should be used is when the source and target languages offer different abstraction levels. If the source and target language offer a similar abstraction level, then the intent of code generation more likely than not becomes to sweep something under the rug. And sweeping things under the rug is never good, imo.

      It makes sense for compilers to translate high level languages into lower level ones. It also makes sense for decompilers to do the reverse. But if you're not jumping across abstraction levels, you really should not be generating any code, imo.

      I don't hold to the above dogmatically/religiously, but when I hear "Java-to-Javascript compilation", then a red flag goes off in my mind.
    • Leo
       
      Posts: 14 / Nickname: aeoo / Registered: April 12, 2006 7:02 AM
      Re: Compiling Java to JavaScript
      December 21, 2006 7:09 PM      
      I'm a little skeptical about this approach. The issue is that with GWT what you debug is not what you deploy. Yes, Java is easier to debug, I agree...but the point easy Java debugging is kind of moot if you don't deploy Java. So the feature is also a problem.

      I think Javascript is a nice enough language that's worth learning and using directly.

      Also, I read somewhere that GWT test browser phones home to Google when you use it. Maybe now that it's open source someone can patch that out. But it's a little fishy in my opinion, if it's true.

      In my experience, frameworks that depend heavily on code-generation are crappy, because they are using generation to hide some kind of flaw that should be addressed directly rather than with a tool. So, my feeling is that this approach is "guilty until proven innocent". So I think I'll let other people be the guinea pigs for this one.

      GWT seems like a heavy-weight framework, with its own test browser, compiler, etc.... You really have to buy it wholesale, it seems. I prefer light-weight yet elegant and clever approaches that are transparent and KISS-compliant. Just because something is written in few lines of code and doesn't come with a compiler doesn't mean it's not powerful. It's all about the amount of thought behind it, not in the amount of code and tools that ships with it.
    • Frank
       
      Posts: 2 / Nickname: fsilber / Registered: March 10, 2006 6:07 AM
      Re: Compiling Java to JavaScript
      December 22, 2006 10:38 AM      
      Leo's concern about code generation reminds me of my early experiences with COBOL. Because of inherent insecurities in the language (e.g. no bounds checking on arrays), debugging sometimes required the ability to read assembly-language-oriented coredumps (write in COBOL but debug in assembly language). However, I do not have this problem with Java, even though the compiler is a code generator for the interpreted bytecode. So the appropriateness of the GWT's approach really depends on how well the abstraction was thought out.

      Leo admits that it's valid to generate code from a higher abstraction level to a lower one. Though Javascript is indeed a high level language, the same cannot be said for XML, HTML and CSS -- which are key part of the browser's execution language. So code generation does seem to me to be the correct approach. There would be a value to this approach even if one doesn't care about AJAX. It has long been established that the abstraction facility offered by full object orientation with inheritance can facilitate code re-use. Browsers require HTML, which is NOT object-oriented. Webmasters deserve an object-oriented notation that eliminates the need to write in non-OO notations such as HTML, CSS and XML.

      Instead of having the developer write in Java to generate the HTML, CSS, XML and client-side JavaScript, I suppose Google could have built tools that let the developer work in (equally high-level) server-side JavaScript. However, server-side JavaScript is much less popular than Java, so again I think they made the right choice.
      • Leo
         
        Posts: 14 / Nickname: aeoo / Registered: April 12, 2006 7:02 AM
        Re: Compiling Java to JavaScript
        December 22, 2006 11:43 AM      
        Frank, I don't think they made a wrong choice, but I don't think it's right either. It's just *a* choice.

        With GWT you cannot innovate client-side. Since all development happens on server side, for example, then the way a table widget looks is set by the GWT. You can't add features to it, because if you wanted to add features to client side widgets, you have to test them...guess where? On client side.

        And a reply to the poster above:

        If I am only allowed to have an opinion after trying something, then what will stop me from trying everything? And if I try everything, then I won't have time to deepen myself in any particular thing. A smart thing to do is not to try everything, but to try only those things that intuition tells you to try. So even before I try it, I evaluate it to see if I should try it. So, it's valid and correct to have an opinion before trying something.

        Is it possible that I can be mistaken? Of course! But mistakes are good and should be encouraged. There is no shame in making a mistake at all. There is nothing good about being correct either.
    • Tali
       
      Posts: 1 / Nickname: talig / Registered: December 23, 2006 3:11 AM
      Re: Compiling Java to JavaScript
      December 23, 2006 9:30 AM      
      I'm a web developer for many years.
      It seems to me , that it would be best if there was an IDE which provided the following:

      1. A real good javascript support (the same you get with other languages , like eclipse for java)

      2. Client side widgets pallete (dojo for instance) with real WISIWYG

      3. An easy integration of your server side objects to your widgets

      4. A "debug mode" and "production mode" (production mode will compress and optimize your javascript)

      I think the place for browser specific code is inside the widgets , dojo , for instance does a beatiful job.

      I think that making the whole suff to be in java is to extreme! I would much rather the aproach I suggested
    • Bril
       
      Posts: 1 / Nickname: brill / Registered: January 22, 2007 4:16 PM
      Re: Compiling Java to JavaScript
      January 22, 2007 10:24 PM      
      Very cool to be able to unit test my javascript code before it ever hits a browser.

      My main complaint is that the tool-chain is very limited in it options (output for instance).

      For instance, a Maven 2 plugin has been developed (can be found in the codehaus sandbox), although its not very mature... however because the compiler is so limited its very hard to get it to do things "The maven way"...
    • Badri
       
      Posts: 1 / Nickname: bnath / Registered: March 8, 2007 2:24 PM
      Re: Compiling Java to JavaScript
      March 8, 2007 8:32 PM      
      GWT converts java code to java script,
      What would be the size of the page when we convert 5kb of java file, because the efficiency of page is always on the loading time.