How to import my extra *.java code to GWT project ?

352 views
Skip to first unread message

Dimedrol

unread,
Feb 5, 2007, 11:04:15 AM2/5/07
to Google Web Toolkit
Hello!

I'm new to GWT, but already has created some simple web applications
using this technology.

I need to have an ability to add to my GWT project some my extra java
sources.
I want to use in my GWT application this code:

/************************************************************/
public final class ClassForGWT {

public ClassForGWT() {
}

public String MyFunction()
{
return "my result";
}
}
/************************************************************/

This is not "pure GWT" class.
But I simply need to call "MyFunction()" from my GWT application.
(create class instance and call a method)
This is NOT widget at all.
I use NetBeans 5.0

How to do this "as simple as possible" ?
Please explain this operation step by step.

Dan Morrill

unread,
Feb 5, 2007, 1:08:14 PM2/5/07
to Google-We...@googlegroups.com

Hi, Dimedrol!

You can use any Java code in a GWT application, provided that it is translatable.  This means that it can't use any classes not provided by the GWT JRE emulation libraries (which generally boils down to:  only java.lang and most of java.util) and it can't (currently) use any Java 5 language features.

If the class you want to use meets those requirements, then you just need to make it accessible to GWT.  "Making it accessible to GWT" means that the class has to be located in a directory specified as containing translatable source code.  You specify these directories in your Module.gwt.xml file, using the <source> XML tag.

The simplest way to do this is to copy the .java file into your client directory as a new class.  If you can't do that, though, you can put it in its own module by creating a ParentModule.gwt.xml file for it, putting the .class, .java, and Module.gwt.xml files in a JAR, and adding that JAR to the classpath for your application.  You would then inherit the new parent module into your GWT application using the <inherits> XML tag.

The documentation on setting up GWT modules is here:  http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.Fundamentals.Modules.html

Hope that helps!

- Dan Morrill

Dimedrol

unread,
Feb 5, 2007, 2:44:15 PM2/5/07
to Google Web Toolkit
Thank you, Dan, for such fast answer!


> The simplest way to do this is to copy the .java file into your client
> directory as a new class.

Yes! The simplest way - is just for me, so far :-)
And it WORKS!
Thank you!

But another, following up question: if I want use the same files with
my class(es) in a several projects, need I to copy these my *.java
files into each "client" directory ?
Or... is there another way to share this one file, without create many
copies of the one *.java class file ?

Jason Essington

unread,
Feb 5, 2007, 3:03:23 PM2/5/07
to Google-We...@googlegroups.com
one method would be to have all of your existing [translatable]
source somewhere and create a module (gwt.xml) for it that points to
the source ...
so maybe have your code in com.me.trans.source
then create the file
com.me.Common.gwt.xml
<module>
<source path="trans/source"/>
<module>

basically, this makes a module that contains your translatable
source, then you can just inherit that module in any new module you
want.

In each of your modules(gwt.xml files) that you need to use that
source, simply add the line:
<inherits name="com.me.Common" />

-jason

Dimedrol

unread,
Feb 6, 2007, 4:33:47 AM2/6/07
to Google Web Toolkit
OK!

> so maybe have your code in com.me.trans.source
> then create the file
> com.me.Common.gwt.xml
> <module>
> <source path="trans/source"/>
> <module>
>

A little bit more explanation, about paths, please...

Actually, I have created a NetBeans' project, called "mygwttools" (my
extra code)
located: "c:\Java\Projects\mygwttools"

It is created like a package, so, after compile I have JAR,
located here: "c:\Java\Projects\mygwttools\dist\mygwttools.jar", but
don't mention it.

My sources are here: "c:\Java\Projects\mygwttools\src\mygwttools"
There are 3 files: gwtSQL.java, MD5.java, TProgressWindow.java

All files are error free, compilable and even includ-able in GWT
project,
like Dan Morrill told me above (copy them into my client directory as
a new class)
My GWT project names "polladmin",
and I have copied files to "c:\Java\Projects\polladmin\src
\GWTApplication\client"
This is works.

So, now, where I have to place (for example, of course) these my 3
files,
where I have to put "com.me.Common.gwt.xml" ? (and how to name this
file?)
What source-tag "<source path="..???..>" I need to create ?
Where to point "path" parameter ?

I understand, that "<inherits name="..." />" needs to be included in
my
GWT project's main XML file, now it is located here - "c:\Java\Projects
\polladm\src\GWTApplication\Main.gwt.xml"

After all, what path in "name" parameter I need to enter ?

By now my Main.gwt.xml looks like :
/******************************************/
<?xml version="1.0" encoding="UTF-8"?>
<module>
<inherits name="com.google.gwt.user.User"/>
<inherits name="mygwttools"/>
<source path="c:\Java\Projects\mygwttools\src\mygwttools"/>
<entry-point class="GWTApplication.client.Main"/>
</module>
/******************************************/

But it is NOT WORKS....

Just on your taste.
Please give a small sample for me, with paths.
Thnak you.

Dimedrol

unread,
Feb 6, 2007, 11:11:19 AM2/6/07
to Google Web Toolkit
Anyone... :-|

Dan Morrill

unread,
Feb 6, 2007, 11:32:19 AM2/6/07
to Google-We...@googlegroups.com

A GWT module is a collection of Java source code and static files.  It is named similarly to a Java package.  A module is defined by a ".gwt.xml" file.  That is, the difference between a JAR file and a GWT module is that a GWT module has a .gwt.xml file.

Suppose you have this source code tree:

src/
    com/
        company/
            util/
                MD5.java
                CheckPassword.java

If you simply compile that and make a JAR file, you would have the following contents in your JAR file:
src/com/company/util/MD5.java
src/com/company/util/MD5.class
src/com/company/util/CheckPassword.java
src/com/company/util/CheckPassword.class

To make this accessible as a GWT module named com.company.GWTUtils, you would need to add this fifth file:
src/com/company/GWTUtils.gwt.xml

The contents of the GWTUtils.gwt.xml file would be something like:
<module>
  <source path="util"/>
</module>

This module configuration tells GWT that the GWTUtils module contains translatable source in the 'utils' subdirectory.  In your application, you would inherit your GWTUtils module like this:
<inherits name="com.company.GWTUtils"/>

Once you have done that, the GWT compiler (and hosted mode) will add the <source> path defined in GWTUtils.gwt.xml from your JAR file into the set of <source> directories for your application.  That is, your com.company.util.CheckPassword and com.company.util.MD5 classes will become accessible in your GWT application.

You can put the .gwt.xml file in the JAR file, and then simply add the JAR file to application project's classpath.  Just be sure to also include the .java source files as well, since GWT needs the source files and not the compiled class files.

This way you can package up common Java code in a JAR file, and not have to have it cluttering your application's source code tree.

- Dan Morrill

Jason Essington

unread,
Feb 6, 2007, 11:37:01 AM2/6/07
to Google-We...@googlegroups.com

On Feb 6, 2007, at 2:33 AM, Dimedrol wrote:

>
> OK!
>
>> so maybe have your code in com.me.trans.source
>> then create the file
>> com.me.Common.gwt.xml
>> <module>
>> <source path="trans/source"/>
>> <module>
>>
>
> A little bit more explanation, about paths, please...
>
> Actually, I have created a NetBeans' project, called "mygwttools" (my
> extra code)
> located: "c:\Java\Projects\mygwttools"
>
> It is created like a package, so, after compile I have JAR,
> located here: "c:\Java\Projects\mygwttools\dist\mygwttools.jar", but
> don't mention it.

Quick note, creating a jar with your gwttools code is a reasonable
thing to do, then you can just add that jar to your classpath in any
project in which you want to use the tools. HOWEVER, you have to
include the source files (.java) in the jar since the GWT compiler
works from source not from bytecode.

> My sources are here: "c:\Java\Projects\mygwttools\src\mygwttools"
> There are 3 files: gwtSQL.java, MD5.java, TProgressWindow.java

I would add a \client directory as well (I think GWT used to have
problems with the default package at one point), and put your files
there:

C:\Java\Projects\mygwttools\src\mygwttools\client\gwtSQL.java etc
then in mygwttools, create:
C:\Java\Projects\mygwttools\src\mygwttools\MyGWTTools.gwt.xml

Since your tools files are already in the .client package, your
Module (gwt.xml file) can be very simple:

<module>
<inherits name="com.google.gwt.user.User"/>

</module>

Then, when you create your jar file, create it with both the .class
files and the .java files (and the MyGWTTools.gwt.xml file) in their
proper path in the jar.

> All files are error free, compilable and even includ-able in GWT
> project,
> like Dan Morrill told me above (copy them into my client directory as
> a new class)
> My GWT project names "polladmin",
> and I have copied files to "c:\Java\Projects\polladmin\src
> \GWTApplication\client"
> This is works.
>
> So, now, where I have to place (for example, of course) these my 3
> files,
> where I have to put "com.me.Common.gwt.xml" ? (and how to name this
> file?)
> What source-tag "<source path="..???..>" I need to create ?
> Where to point "path" parameter ?

Since the step above puts the source in the xxx.client package, you
don't even need the <source> tag anymore. That is primarily for if
you don't want to follow the suggested package structure.

> I understand, that "<inherits name="..." />" needs to be included in
> my
> GWT project's main XML file, now it is located here - "c:\Java
> \Projects
> \polladm\src\GWTApplication\Main.gwt.xml"
>
> After all, what path in "name" parameter I need to enter ?
>
> By now my Main.gwt.xml looks like :
> /******************************************/
> <?xml version="1.0" encoding="UTF-8"?>
> <module>
> <inherits name="com.google.gwt.user.User"/>
> <inherits name="mygwttools"/>
> <source path="c:\Java\Projects\mygwttools\src\mygwttools"/>
> <entry-point class="GWTApplication.client.Main"/>
> </module>
> /******************************************/

Now, put your gwttools.jar file in the classpath (if you compile
using the generated scripts, you'll also need to add it to them.

and in your Main.gwt.xml just add
<inherits name="mygwttools.MyGWTTools"/>
Note: the inherits tag points to the fully qualified name of the
inherited gwt.xml file (minus the .gwt.xml extension)


This is basically the process used for importing the main GWT
libraries, and other third party libraries. so basically you are
creating your own third party library (mygwttools).

-jason

Dimedrol

unread,
Feb 7, 2007, 7:10:45 AM2/7/07
to Google Web Toolkit
Thanks, Jason and Dan for answers,
but after all your instructions I still get an error in Netbeans
compiling...

*******************************************************************
Loading module 'GWTApplication.Main'
Loading inherited module 'mygwttools.MyGWTTools'
[ERROR] Unable to find 'mygwttools/MyGWTTools.gwt.xml' on your
classpath; could be a typo, or maybe you forgot to include a classpath
entry for source?
*******************************************************************
Where this MyGWTTools.gwt.xml must be located ?

Here is a screenshot (11kb. GIF) - http://img209.imageshack.us/
img209/3062/myjarac4.gif
mygwttools.jar included in my project as usual and "inherits" is
mentioned

I have placed my JAR here: c:\Java\widgets\mygwttools.jar
and "c:\Java\widgets" is in SYSTEM'S (!!!) classpath variable.

I have put sources everywhere in my JAR -
see (3kb. GIF) - http://img215.imageshack.us/img215/7083/myjar2gp9.gif

But when I even put my "client" directory with sources inside JAR here
-
c:\Java\widgets\mygwttools.jar\mygwttools\client
and place here "c:\Java\widgets\mygwttools.jar\mygwttools
\MyGWTTools.gwt.xml" too
containing:
*************************************************


<module>
<inherits name="com.google.gwt.user.User"/>

<source path="client"/>
</module>
*************************************************
or without <source path="client"/>

compiler says:

--------------------
init:
deps-jar:
Analyzing source in module 'GWTApplication.Main'
[ERROR] Errors in 'jar:file:/C:/Java/widgets/mygwttools.jar!/
mygwttools/client/MD5.java'
[ERROR] Line 1: The declared package does not match the
expected package mygwttools.client
[ERROR] Errors in 'jar:file:/C:/Java/widgets/mygwttools.jar!/
mygwttools/client/TProgressWindow.java'
[ERROR] Line 1: The declared package does not match the
expected package mygwttools.client
[ERROR] Errors in 'jar:file:/C:/Java/widgets/mygwttools.jar!/
mygwttools/client/gwtSQL.java'
[ERROR] Line 1: The declared package does not match the
expected package mygwttools.client


What I'm doing wrong ???

Jason Essington

unread,
Feb 7, 2007, 10:56:52 AM2/7/07
to Google-We...@googlegroups.com
I'm not sure how Netbeans works, but according to the error, your
mygwttools.MyGWTTools.gwt.xml was not found on the classpath.

This means that the GWTCompiler doesn't have that jar on its
classpath, or your jar doesn't have the gwt.xml file in the right
place. your jar should look something like:

mygwttools.jar
/mygwttools/MyGWTTools.gwt.xml
/mygwttools/client/gwtSQL.java
/mygwttools/client/gwtSQL.class
/mygwttools/client/MD5.java
/mygwttools/client/MD5.class
/mygwttools/client/TProgressWindow.java
/mygwttools/client/TProgressWindow.class

if that is correct, and the jar is on the classpath, then maybe
netbeans needs it on the sourcepath too (just grasping at straws here).

maybe someone with some netbeans experience could chime in.

-jason

Reply all
Reply to author
Forward
0 new messages