0% found this document useful (0 votes)
60 views10 pages

Creating External Jar

Java Tutorial

Uploaded by

Anda Roxana Nenu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views10 pages

Creating External Jar

Java Tutorial

Uploaded by

Anda Roxana Nenu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Google Web Toolkit Creating/using external JAR files

If you develop some code that can be reused in more than one project, one way to create a
module is to create an external JAR file. This JAR file can be included in the build path for
multiple projects.
Required folders/files
The JAR file will need to have the following foldersfiles!
Assume that you have a "roject named MyProj, with a pac#age name of
mydomain.gwt. Also assume that you have the following source files $in your client
directory%, Test.java, and Test&.java. 'our JAR file should contain the following!
META-INF/
META-INF/MANIFEST.MF
mydomain/
mydomain/gwt
mydomain/gwt/client
mydomain/gwt/client/Test.java
mydomain/gwt/client/Test.class
mydomain/gwt/client/Test2.java
mydomain/gwt/client/Test2.class
mydomain/gwt/MyProj.gwt.xml
(oth the source $.java% and the bytecode $.class% files should be in the client folder.
$They could be stored in other folders if the appropriate changes are made to the
module.gwt.xml file, but this is not usually done.% The source files must be present to be
compiled to Java)cript.
Format for source files
Assuming the project and pac#age structure above, the source file should have the following
format!
!ac"age mydomain.gwt.client#
// im!ort statements
!$%lic class Test
&
// code goes 'ere
(
Contents of MyPro!g"t!xml
The module.gwt.xml file should not contain any entry points. All it needs is the inherits for
any modules that are used. *or example!
)mod$le*
+
)in'erits name+,com.google.gwt.$ser.-ser,/*
)/mod$le*
#sing t$e external JAR
To use the external JAR file, you modify the project,s Java (uild "ath, by going to the
-ibraries tab, selecting .xternal JARs, and selecting the JAR file that you create.
In addition, in your module.gwt.xml file, you need to reference the project file as in!
)mod$le*
).-- In'erit t'e core /e% Tool"it st$00 --*
)in'erits name+1,com.google.gwt.$ser.-ser,/1*
).-- 22 A33 T4IS 22 --*
)in'erits name+1,mydomain.gwt.MyProj,/1*
).-- S!eci0y t'e a!! entry !oint class. --*
)entry-!oint
class+1,mydomain.gwt.client.MyA!!lication/1*
)/mod$le*
Example: GWTCanvasHelpers
To give a clearer idea of how to do this, let,s go through an actual example. In this case, let,s
create a module that will help to draw arrows on a /0T1anvas object.
In .clipse, go to *ile234ew230eb Application "roject. 1hoose 5/0T1anvas6elpers7
for the project name, and 5edu.hawaii.ta#ebaya.gwt7 for the pac#age name. 8eselect the
/oogle App .ngine chec#box.
In the src folder, delete all the /reeting )ervice code that the /0T plugin inserts. That is,
go to the srceduhawaiita#ebayagwtclient folder and delete /reeting)ervice.java and
/reeting)erviceAsync.java. Also, go to srceduhawaiita#ebayagwtserver and delete
/reeting)erviceImpl.java.
9odify /0T1anvas6elper.gwt.xml to contain!
)5xml version+16.71 encoding+1-TF-815*
)mod$le rename-to+,gwtcanvas'el!ers,*
)in'erits name+,com.google.gwt.widgetideas.9/T:anvas,/*
)/mod$le*
In the srceduhawaiita#ebayagwtclient folder, change the contents of
/0T1anvas6elpers.java to the following!
!ac"age ed$.'awaii.ta"e%aya.gwt.client#
im!ort com.google.gwt.widgetideas.gra!'ics.client.9/T:anvas#
!$%lic class 9/T:anvas4el!ers
&
&
1
2
3
4
5
6
!$%lic static void draw;ine<Point a= Point %= 9/T:anvas canvas>
&
canvas.%eginPat'<>#
canvas.moveTo<a.x= a.y>#
canvas.lineTo<%.x= %.y>#
canvas.stro"e<>#
(
!$%lic static void drawArrow<Point a= Point %= 9/T:anvas canvas>
&
do$%le delta? + %.x - a.x#
do$%le delta@ + a.y - %.y#
do$%le dirAadians + Mat'.atan2<delta@= delta?>#
do$%le dir3egrees + dirAadians2687.7/Mat'.PI#
do$%le dirB!!osite + dir3egrees C 687.7#
w'ile <dirB!!osite * DE7> &
dirB!!osite + dirB!!osite - DE7#
(
w'ile <dirB!!osite ) 7> &
dirB!!osite + dirB!!osite C DE7#
(
do$%le delta?6 + 672Mat'.cos<<dirB!!ositeC27>2Mat'.PI/687.7>#
do$%le delta@6 + 672Mat'.sin<<dirB!!ositeC27>2Mat'.PI/687.7>#
do$%le delta?2 + 672Mat'.cos<<dirB!!osite-27>2Mat'.PI/687.7>#
do$%le delta@2 + 672Mat'.sin<<dirB!!osite-27>2Mat'.PI/687.7>#
Point newPoint6 + new Point<%.xCdelta?6=%.y-delta@6>#
Point newPoint2 + new Point<%.xCdelta?2=%.y-delta@2>#
draw;ine<a=%=canvas>#
draw;ine<%=newPoint6=canvas>#
draw;ine<%=newPoint2=canvas>#
(
(
This class will re:uire the "oint class. Right;clic# on the
srceduhawaiita#ebayagwtclient folder and select 4ew231lass. 4ame the class "oint,
and ma#e it have the following contents!
!ac"age ed$.'awaii.ta"e%aya.gwt.client#
!$%lic class Point
&
!$%lic do$%le x#
!$%lic do$%le y#

!$%lic Point<do$%le x= do$%le y>
&
t'is.x + x#
t'is.y + y#
(
(
Creating t$e JAR file using %cli&se
Right;clic# on the project in the "ac#age .xplorer. )elect .xport23Java23JAR file. )ee
*igure + on page <.
=
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1
2
3
4
5
6
7
8
9
10
11
12
13
1lic# 4ext. As show in *igure & on page >, Turn off all the chec# boxes except for src for
the resources to export. Turn on the chec#box 5.xport Java source files and resources7 so
that the Java source code is included in the JAR file. )elect the export destination and
filename. Then, clic# 4ext.
<
Figure 1: Screen shot showing start of procedure to export to JAR.
After clic#ing 4ext, the following page is shown. )ee *igure = on page ?. @eep the default
options of exporting clas files with compile errors and warnings as shown.
>
Figure 2: Screen shot showing resources to export to JAR.
1lic# 4ext. As shown in *igure < on page A, select 5/enerate the manifest file7. )ince this
is not an executable jar, do not set the 9ain class. 1lic# *inish.
?
Figure 3: Screen shot showing the JAR packaging options.
The resulting JAR file loo#s li#e this!
A
Figure 4: Screen shot showing JAR manifest specification.
#sing t$e JAR file in anot$er &roect
The following is a simple example of using this JAR file in a /0T project. In .clipse, start by
going to *ile234ew230eb Application "roject. )et the project name to 5Test8raw7, and
the pac#age name to 5vern.test7. Turn off the /oogle App .ngine chec#box.
8elete all the references to the /reeting )ervlet. That is delete all the Java files that are put
in by the plugin that contain )ervlet $/reeting)ervlet.java, /reeting)ervletAsync.java, and
/reeting)ervletImpl.java%.
9odify 5Test8raw.gwt.xml7 to contain!
)5xml version+16.71 encoding+1-TF-815*
)mod$le rename-to+,testdraw,*
).-- In'erit t'e core /e% Tool"it st$00. --*
)in'erits name+,com.google.gwt.$ser.-ser,/*
)in'erits name+,com.google.gwt.widgetideas.9/T:anvas,/*
)in'erits name+,ed$.'awaii.ta"e%aya.gwt.9/T:anvas4el!ers,/*
).-- In'erit t'e de0a$lt 9/T style s'eet. --*
)in'erits name+,com.google.gwt.$ser.t'eme.standard.Standard,/*
).-- S!eci0y t'e a!! entry !oint class. --*
)entry-!oint class+,vern.text.client.Test3raw,/*
)/mod$le*
The #ey line $shown in bold% is line ?. 4ote how the pac#age name followed by the
module name is how we reference the module.
9odify 5Test8raw.html7 to contain!
).docty!e 'tml*
)'tml*
)'ead*
)meta 'tt!-eF$iv+1content-ty!e1 content+1text/'tml# c'arset+-TF-81*
)lin" ty!e+1text/css1 rel+1styles'eet1 're0+1Test3raw.css1*
)title*3emo o0 9/T:anvas4el!ers)/title*
)scri!t ty!e+1text/javascri!t1 lang$age+1javascri!t1
src+1testdraw/testdraw.nocac'e.js1*)/scri!t*
)/'ead*
)%ody*
).-- AE:BMMEN3E3 i0 GavaScri!t m$st %e ena%led --*
)noscri!t*
)div style+1widt'H 22em# !ositionH a%sol$te# le0tH I7J#
margin-le0tH -66em# colorH red# %ac"gro$nd-colorH w'ite#
%orderH 6!x solid red# !addingH K!x# 0ont-0amilyH sans-seri01*
@o$r we% %rowser m$st 'ave GavaScri!t ena%led
in order 0or t'is a!!lication to dis!lay correctly.
)/div*
)/noscri!t*
)'6*Test o0 9/T:anvas4el!ers)/'6*
)/%ody*
)/'tml*
9odify 5Test8raw.css7 to contain!
B
1
2
3
4
5
6
7
8
9
10
11
12
/22 Add css r$les 'ere 0or yo$r a!!lication. 2/
/22 Exam!le r$les $sed %y t'e tem!late a!!lication <remove 0or yo$r
a!!> 2/
'6 &
0ont-siLeH 2em#
(
9odify 5web.xml7 to contain!
)5xml version+16.71 encoding+1-TF-815*
).3B:T@PE we%-a!!
P-M;I: 1-//S$n Microsystems= Inc.//3T3 /e% A!!lication 2.D//EN1
1'tt!H//java.s$n.com/dtd/we%-a!!N2ND.dtd1*
)we%-a!!*
).-- 3e0a$lt !age to serve --*
)welcome-0ile-list*
)welcome-0ile*Test3raw.'tml)/welcome-0ile*
)/welcome-0ile-list*
)/we%-a!!*
Add the JAR file, 5gwtcanvas;helpers.jar7 to the project,s Java (uild "ath property. Add
the gwt;incubator;&C+C&C<;r+A<A.jar file as well.
*inally, modify 5Test8raw.java7 to contain the following!
!ac"age vern.text.client#
im!ort com.google.gwt.core.client.EntryPoint#
im!ort com.google.gwt.$ser.client.$i.AootPanel#
im!ort com.google.gwt.$ser.client.$i.OerticalPanel#
im!ort com.google.gwt.widgetideas.gra!'ics.client.9/T:anvas#
im!ort ed$.'awaii.ta"e%aya.gwt.client.9/T:anvas4el!ers#
im!ort ed$.'awaii.ta"e%aya.gwt.client.Point#
!$%lic class Test3raw im!lements EntryPoint
&
!$%lic void onMod$le;oad<>
&
OerticalPanel mainPanel + new OerticalPanel<>#
9/T:anvas canvas + new 9/T:anvas<K77=K77>#
mainPanel.add<canvas>#
Point 0rom + new Point<D7=D7>#
Point to + new Point<677=677>#
9/T:anvas4el!ers.drawArrow<0rom= to= canvas>#
Point next + new Point<277=I7>#
9/T:anvas4el!ers.drawArrow<to= next= canvas>#
AootPanel.get<>.add<mainPanel>#
(
(
4ote on lines A and B how we import the classes for use. -ines +D and &+ draw the arrows
D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using the "oints that are set up on lines +A, +B, and &C. The resulting web application has the
following appearance!
+C
Figure : Screen shot showing simp!e we" app!ication that makes use of drawArrow#$ method.

You might also like