Compiling a single Java class to a plain JavaScript file

1,343 views
Skip to first unread message

rapodaca

unread,
Jan 6, 2009, 4:13:41 PM1/6/09
to Google Web Toolkit
Greetings,

Is it possible to use GWT to convert a single Java class to a
JavaScript file that can be used within a browser? If so, how?

More specifically, if I have a file called Hello.java containing:

public class Hello
{
public String talk()
{
return "hello";
}
}

how can I use GWT to convert it into a single file called hello.js
that could be used to instantiate a Hello object and call it's talk
method?

Almost all of the documentation on GWT I've been able to find deals
with creating a Web site. I'd like to do something much simpler - just
convert my Java class to a Javascript class-like structure that can
instantiate an object and invoke its methods.

This seems like something that should be trivial, but I've had no luck
finding out how to get it done.

Many thanks,
Rich

Ziyod

unread,
Jan 6, 2009, 8:58:38 PM1/6/09
to Google Web Toolkit
Use GWTCompiler it's part of the com.google.gwt.dev.GWTCompiler
package
Create a gwtCompiler.cmd file and insert this command:
@java -cp "%~dp0\gwt-user.jar;%~dp0\gwt-dev-windows.jar"
com.google.gwt.dev.GWTCompiler %*

GWTCompiler [-logLevel level] [-gen dir] [-out dir] [-treeLogger] [-
style style] module
where
-logLevel The level of logging detail: ERROR, WARN, INFO, TRACE,
DEBUG, SPAM, or ALL
-gen The directory into which generated files will be written
for review
-out The directory to write output files into (defaults to
current)
-treeLogger Logs output in a graphical tree view
-style Script output style: OBF[USCATED], PRETTY, or DETAILED
(defaults to OBF)
and
module Specifies the name of the module to compile

? OBF—Obfuscated mode. This is a non-human-readable, compressed
version suitable for production use.
? PRETTY—Pretty-printed JavaScript with meaningful names.
? DETAILED—Pretty-printed JavaScript with fully qualified names.

Find out more:
http://www.screaming-penguin.com/GWTDocs

rapodaca

unread,
Jan 7, 2009, 1:20:12 AM1/7/09
to Google Web Toolkit
On Jan 6, 5:58 pm, Ziyod <ziyod2...@gmail.com> wrote:
> Use GWTCompiler it's part of the com.google.gwt.dev.GWTCompiler
> package
> Create a gwtCompiler.cmd file and insert this command:
> @java -cp "%~dp0\gwt-user.jar;%~dp0\gwt-dev-windows.jar"
> com.google.gwt.dev.GWTCompiler %*

Hello Ziyod,

Thanks for the information. I'm on Linux, but my best guess for
translation is (creating file GWT_INSTALL/gwtCompiler):

java -cp $HOMEDIR/gwt-user.jar:$HOMEDIR/gwt-dev-linux.jar
com.google.gwt.dev.GWTCompiler "$@";

This is based on the projectCreator script provided in the
installation.

I notice that HOMEDIR is not set so I go:

$ export HOMEDIR=~/tmp/gwt-linux-1.5.3

Then I try:

$ ./gwtCompiler com.example.Test
Loading module 'com.example.Test'
[ERROR] Unable to find 'com/example/Test.gwt.xml' on your
classpath; could be a typo, or maybe you forgot to include a classpath
entry for source?
[ERROR] Build failed

I'm not sure what happened or what the com/example/Test.gwt.xml file
refers to. Any ideas of how to generate it and where to save it?
That's a good command summary, but unfortunately, I don't see any
example usage.

Reinier Zwitserloot

unread,
Jan 7, 2009, 3:20:03 AM1/7/09
to Google Web Toolkit
rapodaca: The GWTCompiler is simply what drives the myProject-compile
command; it does the same thing (translates a whole bunch of things
into a single JS file), so I doubt its what you're looking for.

What you want can't really be done. Java isn't javascript. Take your
example just to illustrate the problem here: How would that even
translate to javascript? Javascript doesn't have classes. It has
prototype based inheritance. Here are your fundamental problems:

1. Javascript's namespacing and object model is so different, that
GWT internally generates completely different and unwieldy names for
objects and classes. These names are then mangled to unrecognizable
shortcodes to reduce the size of the output JS. So, your public void
hello method is either going to be called: "com.mypackage.Hello::talk
()" (yes, including closing parentheses to indicate that this version
takes no parameters; unlike javascript, in java two methods with the
same name but different parameter lists are completely separate, in
javascript you can't do that), or it's going to be called something
small and effectively random, so something like 'xYq' or some such.
There's no code to pick a sane name for interaction, so nothing there
that would even think to generate just a function called "hello".

2. There's a base set of functions that all GWT projects start out
with. The GWT compiler assumes this basis is there.

3. GWT does something called platform targeting. That's why it
generates a number of JS files - one for each target platform. Out of
the box, there are already multiple platforms (1 for each major
supported browser, so there's an Opera, an IE, a Gecko, and a Webkit).
I'm not entirely sure but I believe the base, talked about in #2, is
already written specifically for each target browser platform.


If you are in the market to build such a tool, The GWT sources are a
great place to start, but unless you're willing to dig in for a few
weeks and do a lot of dev work, I don't think GWT can do what you
want.

NB: I'm not an expert on the GWT internals so I might have made a few
mistakes, but I'm fairly sure the above is true. #1 is certainly true,
and already a big deal for you.

Ziyod

unread,
Jan 7, 2009, 5:19:12 PM1/7/09
to Google Web Toolkit
Hi Rich,
Reinier is right about the GWTCompiler.
I thought you were trying to get human-readable javascript. I run this
command in Windows(notice -style DETAILED argument):
@java -Xmx256M -cp "%~dp0\src;%~dp0\bin;%~dp0\../../gwt-user.jar;
%~dp0\../../gwt-dev-windows.jar" com.google.gwt.dev.GWTCompiler -out
"%~dp0\www" -style DETAILED %* com.google.gwt.sample.hello.Hello

rapodaca

unread,
Jan 7, 2009, 7:26:11 PM1/7/09
to Google Web Toolkit


On Jan 7, 12:20 am, Reinier Zwitserloot <reini...@gmail.com> wrote:
> What you want can't really be done. Java isn't javascript. Take your
> example just to illustrate the problem here: How would that even
> translate to javascript? Javascript doesn't have classes. It has
> prototype based inheritance. Here are your fundamental problems:

Reinier, thanks for the info. Actually, a class isn't the thing I'm
after. What I really want to create are objects. So I thought it could
translate something like this:

<html>
...
<script src="hello.js"></script>
...
<script type="text/javascript">
var hello = new Hello();

hello.talk // returns "hello"
</script>
...
</html>

What am I missing?

>  1. Javascript's namespacing and object model is so different, that
> GWT internally generates completely different and unwieldy names for
> objects and classes. These names are then mangled to unrecognizable
> shortcodes to reduce the size of the output JS. So, your public void
> hello method is either going to be called: "com.mypackage.Hello::talk
> ()" (yes, including closing parentheses to indicate that this version
> takes no parameters; unlike javascript, in java two methods with the
> same name but different parameter lists are completely separate, in
> javascript you can't do that), or it's going to be called something
> small and effectively random, so something like 'xYq' or some such.
> There's no code to pick a sane name for interaction, so nothing there
> that would even think to generate just a function called "hello".

I thought it was possible to turn off the mangling by setting compiler
flags. I've also read about JSNI in this context. Wouldn't one or both
of these help?

>  2. There's a base set of functions that all GWT projects start out
> with. The GWT compiler assumes this basis is there.

That wouldn't be a problem for me.

>  3. GWT does something called platform targeting. That's why it
> generates a number of JS files - one for each target platform. Out of
> the box, there are already multiple platforms (1 for each major
> supported browser, so there's an Opera, an IE, a Gecko, and a Webkit).
> I'm not entirely sure but I believe the base, talked about in #2, is
> already written specifically for each target browser platform.

Also not a problem.

> If you are in the market to build such a tool, The GWT sources are a
> great place to start, but unless you're willing to dig in for a few
> weeks and do a lot of dev work, I don't think GWT can do what you
> want.

Sounds way above my head. I have to say, though - I'm very surprised
this hasn't been done already. It seems like such an obvious use of
GWT.

Ian Petersen

unread,
Jan 8, 2009, 11:50:44 AM1/8/09
to Google-We...@googlegroups.com
On Wed, Jan 7, 2009 at 4:26 PM, rapodaca <rich.a...@gmail.com> wrote:
> On Jan 7, 12:20 am, Reinier Zwitserloot <reini...@gmail.com> wrote:
>> What you want can't really be done. Java isn't javascript. Take your
>> example just to illustrate the problem here: How would that even
>> translate to javascript? Javascript doesn't have classes. It has
>> prototype based inheritance. Here are your fundamental problems:
>
> Reinier, thanks for the info. Actually, a class isn't the thing I'm
> after. What I really want to create are objects. So I thought it could
> translate something like this:
>
> <html>
> ...
> <script src="hello.js"></script>
> ...
> <script type="text/javascript">
> var hello = new Hello();
>
> hello.talk // returns "hello"
> </script>
> ...
> </html>
>
> What am I missing?

It seems to me that you're missing the fact that classes are the
breeding ground for objects (at least in Java, anyway). Saying "I'm
not after classes--all I want are objects" is a bit like saying "I'm
not after seeds--all I want are flowers".

You _might_ be able to get what you want with Ray Cromwell's Exporter.
I forget where you can find it, but it's available on the web
somewhere. It might be hosted as a Google Code project. You could
probably find it by searching this list's history for posts by Ray
Cromwell that mention "Exporter". The Exporter basically allows you
to mark certain classes and interfaces as "exportable". Exported
interfaces get wrapped in a bunch of JSNI goo to make it easy for a
traditional Javascript script to call into the compiled GWT output.

> I thought it was possible to turn off the mangling by setting compiler
> flags. I've also read about JSNI in this context. Wouldn't one or both
> of these help?

JSNI can help, and the Exporter automates the drudgery for you.
Turning off name mangling doesn't help, it just makes it possible for
a human to read the compiler's output and make associations between
the compiled code and the Java source that it came from.

Ian

rapodaca

unread,
Jan 8, 2009, 1:32:05 PM1/8/09
to Google Web Toolkit
On Jan 8, 8:50 am, "Ian Petersen" <ispet...@gmail.com> wrote:

> You _might_ be able to get what you want with Ray Cromwell's Exporter.
>  I forget where you can find it, but it's available on the web
> somewhere.  It might be hosted as a Google Code project.  You could
> probably find it by searching this list's history for posts by Ray
> Cromwell that mention "Exporter".  The Exporter basically allows you
> to mark certain classes and interfaces as "exportable".  Exported
> interfaces get wrapped in a bunch of JSNI goo to make it easy for a
> traditional Javascript script to call into the compiled GWT output.

Ian, many thanks. After a quick search, I found these resources:

Announcement of Exporter project:

http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/201528f512c546f3

GWT Exporter Google Code Page:

http://code.google.com/p/gwt-exporter/

At first glance, this looks like exactly what I was after.

> > I thought it was possible to turn off the mangling by setting compiler
> > flags. I've also read about JSNI in this context. Wouldn't one or both
> > of these help?
>
> JSNI can help, and the Exporter automates the drudgery for you.
> Turning off name mangling doesn't help, it just makes it possible for
> a human to read the compiler's output and make associations between
> the compiled code and the Java source that it came from.

Very nice. It looks like the documentation is pretty thin, but the
announcement has enough info to at least get started.

If anyone has any additional info on GWT-Exporter or Exporter-like
packages, I'd be very interested to hear about them.

Thanks,
Rich

Reinier Zwitserloot

unread,
Jan 10, 2009, 6:27:53 AM1/10/09
to Google Web Toolkit
rapodaca:

Reread my original post. Even if you turn the mangling off, the name
would then become:

com.mypackage.MyClass::hello()

Instead of what you probably want:

hello


Due to the parens, you'd need to call it like so:

window.["com.mypackage.MyClass::hello()"]();

instead of like so:

hello();

'unwieldly' doesn't really cover the ridiculousness of working with
this. 'utterly ridiculous' is a bit closer to the mark. The above is
WITHOUT the mangling. -style pretty is a bit less unwieldy, but,
really, you'd have to get in there and write your own identifier
formatter that does saner things, and that's just one thing that comes
to mind. I'm fairly sure its not going to be the only hurdle.


On Jan 8, 1:26 am, rapodaca <rich.apod...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages