< Zurück | Inhalt | Weiter >

22.2.6 Compiling Your Beans

In order to compile these bean-related Java classes, you need to have a J2EE JAR in your classpath. If you’ve installed JBoss into /usr/local/jboss, you could add the JAR to your classpath this way:

export CLASSPATH="/usr/local/jboss/client"\ "/jboss-j2ee.jar:.:${CLASSPATH}"

If you have the Sun J2EE reference implementation installed on your system (in /usr/local), then you could use:

export CLASSPATH="/usr/local/SUNWappserver"\ "/lib/j2ee.jar:.:${CLASSPATH}"

If you have Geronimo installed on your system (with an environment variable GHOME to hold its location), then you would use:1

export CLASSPATH="${GHOME}/repository/geronimo-spec/jars"\ "/geronimo-spec-j2ee-1.0-M1.jar:.:${CLASSPATH}"

In any case, the point is to have in your classpath the JAR file which contains the javax/ejb/ classes, such as EJBObject.class. If you haven’t installed one of these containers on your machine, then download a copy of the JAR from the machine where that container is installed. Put your copy somewhere in your classpath, as in the examples above.


1. Our JAR is named geronimo-spec-j2ee-1.0-M1.jar but yours will likely have a dif- ferent name by the time the J2EE-certified version of Geronimo is available. It may likely just have the trailing -M1 dropped from the name; check in the geronimo-spec/jars directory. Another option is to use any of the J2EE JARs, for example, from the Sun reference implemen- tation. That’s the advantage of standards. Any of them should work for this compilation step. The resulting compiled code should be deployable to any server. “Compile once, run anywhere,” right?

If you are using Ant (and why wouldn’t you be?) you will need to put the path to the J2EE JAR in the classpath which Ant uses. Since Ant can define its own classpath, you may want to define your compile task as something like this:

<target name="compile" >

<javac srcdir="${src}" destdir="${build}"

classpath="/usr/local/SUNWappserver/lib/j2ee.jar:${stdpath}" />


Now it’s a simple matter to compile. The basic Java compile command:

$ javac com/jadol/budgetpro/*.java

will compile all the various classes and interfaces that make up the EJB. There may be other classes in your source tree that need to be compiled as well. With all that going on, you can see why so many people use Ant. With the compile target defined as in our example above, you would need only the command:

$ ant compile


There are many pieces involved in the construction of an EJB. Besides writing the session bean implementation, there are the remote and home interfaces to be written. The local and local home interfaces are optional, but useful, espe- cially in cases where arguments cannot be serialized or where you know that the beans and the client will reside on the same host. Compiling an EJB is not different from any other Java compile, but it requires a special JAR in your classpath, one that contains the definitions of the J2EE objects and interfaces.


We still haven’t shown you how to deploy the EJB, now that you have one compiled. In the next chapter we’ll show you how to assemble the pieces of the EJB into an Enterprise Archive and then deploy and run it.

More importantly, though, we haven’t covered how to write the code for stateful session beans or entity beans. Stateful session beans are one-to-a-client- session, rather than taking all comers, so there is a little more to code, and a


22.5 Resources 503

few small changes required in the ejb-jar.xml config file. (What is the ejb-jar.xml config file? It is part of the configuration information used to deploy your EJB, the topic of the next chapter.) But there are some serious complications with entity beans that involve retrieving the bean’s data from a “backing store”—for example, a database—and writing it back. There are many good references on these topics, and we mention our favorites in the next section.


These are some of our favorite resources for learning about and dealing with EJBs. All of these give much more extensive examples that we have space for, and we encourage you to look at one or more of them.

J2EE and Beyond by Art Taylor (Prentice Hall PTR, ISBN 0-13-141745-2) gives a very good overview of all of the pieces of J2EE. At over 1,000 pages, it’s no small book, but it covers a lot more than just EJBs.

Enterprise JavaBeans Component Architecture: Designing and Coding Enter- prise Applications by Gail Anderson and Paul Anderson, Sun Microsystems Press, ISBN 0-13-035571-2. At only 435 pages it is the most concise of the three, with the tightest focus and an emphasis on the code.

Applied Enterprise JavaBeans Technology by Kevin Boone, Sun Microsystems Press, ISBN 0-13-044915-6. At just over 700 pages, it is midway between the other two titles. Like the Taylor book, it covers some related technolo- gies, but gives more depth to EJBs than Taylor, as that is its focus. It pro- vides more examples than the Andersons, but its examples are not any deeper, just broader.