views:

668

answers:

2

Given a set of Java source code files, how can I compile them into one or more JavaScript files that can be used with hand-crafted JavaScript?

GWT is one option, but every example I've seen so far is aimed at building fancy websites. The simple use case of just converting Java source to Javascript that can be used together with handcrafted JavaScript hasn't been well-documented.

I started a thread on the GWT mailing list on this subject, but opinions seem to be mixed on whether this is even feasible.

One person gave a very useful tip, which was to check out GWT-Exporter. The problem is that neither source code nor documentation is readily available, although there's this and this.

edit: GWT-Exporter source code is here

I've also seen Java2Script. But again, I wasn't able to find examples of how to solve my simple use case.

What's the best approach to this problem? Is there something better I'm missing?

A: 

Aside from syntactical similarities there's really not a whole lot that Java and javascript have in common. What you're asking is a bit like "I want to play Half Life on my calculator. Is there a tool that will convert it for me?"

Spencer Ruport
That's not fair at all. GWT actually does what the OP wants, but I guess it's just too big/complicated for his needs.
Outlaw Programmer
Well without further background I can only assume that he's trying to get a multithreaded Java application converted into javascript.
Spencer Ruport
@Outlaw, that's my understanding of what GWT does as well. The way it's normally documented is just overkill for and too much to wade through.@Spencer, not at all. A simple HelloWorld would do.
Rich Apodaca
So technically, the question is not 'how do I do it', it is 'someone read the instructions for me'...
Geoffrey Chetwood
@Rich B, I'd read the instructions if I could find them. There aren't any on this subject that I could find. If you have a link/source, I'd be interested in reading it.
Rich Apodaca
@Rich A: http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5
Geoffrey Chetwood
+2  A: 

When you use GWT, you're basically converting the UI portion into Javascript (and it assumes that you use the UI widgets provided when you write your Java). Only some of the Java libraries are accessible within Javascript. Typically in a GWT application anything that makes heavy use of Java libraries would run on the server side and connect to the Javascript as AJAX (which GWT handles for you). So GWT isn't necessarily converting your full application into Javascript (though it can if you're willing to limit your use of Java libraries and some functionality).

At any rate, if this approach (calling out to Java running on a server from within Javascript) appeals to you, one nice option is DWR, which basically allows your Javascript to directly call methods in Java classes running on the server (without you having to build a web service or other frontend). Not what you asked, I know.

More relevantly, it looks like there's source code for a sample app demonstrating the use of gwt-exporter.

JacobM
This would be for a fairly small, non-gui java library. I hadn't seen DWR, which looks like it could be very useful for larger projects.I Didn't even see the source directory for the gwt-exporter project - thanks for pointing it out. That looks promising.
Rich Apodaca