A true newbie -- Please help!
rij Mar 10, 2009 2:40 PMHello,
I am new to the whole world of Java, JEE, EJB, XML, JBoss. I am trying to code up the simple example that is provided in Chapter 4 of the book Enterprise JavaBeans 3.0. I am having a few problems. I hope to get some pointers here.
The project has 4 files:
a) An entity bean: User
b) A stateless session bean: UserRegistrationSessionBean
c) A remote interface: UserRegistrationRemote
d) A client file that creates and then looks up a user.
--------------------------------------------------------------------------
First my setup:
Java: jdk1.6.0_12
JBoss: jboss-5.0.0-GA
Ant: apache-ant-1.7.1
OS: Windows XP
All my java files, build files are in the same directory.
--------------------------------------------------------------------------
Second, the problem.
When I use the build.xml file, the compiler complains:
jo\ejb\build.xml:21: The element type "path" must be terminated by the matching end-tag "<//path>".
The corresponding XML code snippet in build.xml (copied from book) is:
<path id="classpath"> <fileset dir="${basedir}"/> <include name="*.jar"/> </fileset> <pathelement location="${build.classes.dir}"/> <pathelement location="${basedir}/client-config"/> </path>
Now, when I change the above to:
<path id="classpath"> <pathelement location="${basedir}"/> <=== Changed this <pathelement location="${build.classes.dir}"/> <pathelement location="${basedir}/client-config"/> </path>
The build proceeds but I get the following compiler errors.
C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.0.GA\apps\mo
jo\ejb>ant
Buildfile: build.xml
prepare:
[mkdir] Created dir: C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jd
k6\jboss-5.0.0.GA\apps\mojo\ejb\build
[mkdir] Created dir: C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jd
k6\jboss-5.0.0.GA\apps\mojo\ejb\build\classes
compile:
[javac] Compiling 4 source files to C:\Documents and Settings\Dan\Desktop\jb
oss-5.0.0.GA-jdk6\jboss-5.0.0.GA\apps\mojo\ejb\build\classes
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\UserRegistrationRemote.java:5: '.' expected
[javac] import User;
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\UserRegistrationRemote.java:5: ';' expected
[javac] import User;
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\UserRegistrationRemote.java:7: class, interface, or enum expe
cted
[javac] @Remote
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\UserRegistrationSessionBean.java:8: '.' expected
[javac] import Cabin;
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\UserRegistrationSessionBean.java:8: ';' expected
[javac] import Cabin;
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\UserRegistrationSessionBean.java:10: class, interface, or enu
m expected
[javac] @Stateless
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\client.java:1: '.' expected
[javac] import UserRegistrationRemote;
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\client.java:1: ';' expected
[javac] import UserRegistrationRemote;
[javac] ^
[javac] C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.
0.GA\apps\mojo\ejb\client.java:2: class, interface, or enum expected
[javac] import User;
[javac] ^
[javac] 9 errors
BUILD FAILED
C:\Documents and Settings\Dan\Desktop\jboss-5.0.0.GA-jdk6\jboss-5.0.0.GA\apps\mo
jo\ejb\build.xml:38: Compile failed; see the compiler error output for details.
--------------------------------------------------------------------------
Finally the code:
There are 5 files:
1) User.java:
import javax.persistence.*; @Entity @Table(name="USER") public class User implements java.io.Serializable { private String id; private String email; private String password; private String affiliation; @Id @Column(name="EMAIL") public String getEmail() { return email; } public void setEmail(String new_email) { email = new_email; } @Column(name="ID") public String getId() { return id; } public void setId(String new_id) { id = new_id; } @Column(name="PASSWORD") public String getPassword() { return password; } public void setPassword(String new_password) { password = new_password; } @Column(name="AFFILIATION") public String getAffiliation() { return affiliation; } public void setAffiliation(String new_affiliation) { affiliation = new_affiliation; } }
--------------------------------------------------------------------------
2) UserRegistrationRemote.java
import javax.ejb.Remote; import User; @Remote public interface UserRegistrationRemote { public void registerUser(User user); public User findUser(String email); }
--------------------------------------------------------------------------
3) UserRegistrationSessionBean.java
import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import User; @Stateless public class UserRegistrationSessionBean implements UserRegistrationRemote { @PersistenceContext (unitName="user") private EntityManager manager; public void registerUser(User user) { manager.persist(user); } public User findUser(String email) { return manager.find(User.class, email); } }
--------------------------------------------------------------------------
4) client.java
import UserRegistrationRemote; import User; import javax.naming.IntialContext; import javax.naming.Context; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; public class Client { public static void main (String[] args) { try { Context jndiContext = getInitialContext(); Object ref = jndiContext.lookup("UserRegistrationSessionBean/remote"); UserRegistrationRemote dao = (UserRegistrationRemote)PortableRemoteObject.narrow(ref, UserRegistrationRemote.class); User usr = new User(); usr.setEmail("world@peace.com"); usr.setId("us"); usr.setPassword("test"); usr.setAffiliation("world"); dao.registerUser(usr); User res_usr = dao.findUser("world@peace.com"); System.out.println("Email: " + res_usr.getEmail()); System.out.println("Id: " + res_usr.getId()); System.out.println("Password: " + res_user.getPassword()); System.out.println("Affiliation: " + res_usr.getAffiliation()); } catch (javax.naming.NamingException ne) { ne.printStackTrace(); } } public static Context getInitialContext() throws javax.naming.NamingException { return new javax.naming.InitialContext(); } }
--------------------------------------------------------------------------
5) build.xml
<?xml version="1.0"?> <!-- Build file for first project for of JBoss --> <project name="EJB Build for Mojo" default="ejbjar" basedir="."> <!-- Standard Properties --> <property environment="env"/> <property name="src.dir" value="${basedir}"/> <property name="lib.dir" value="${basedir}/lib"/> <property name="jboss.dir" value="${env.JBOSS_HOME}"/> <property name="jboss.deploy.dir" value="${jboss.dir}/server/default/deploy"/> <property name="build.dir" value="${basedir}/build"/> <property name="build.classes.dir" value="${build.dir}/classes"/> <!-- Next, specify where the client app is. client-config which has JNDI properties --> <path id="classpath"> <pathelement location="${basedir}"/> <pathelement location="${build.classes.dir}"/> <pathelement location="${basedir}/client-config"/> </path> <!-- Create directories to place compiled files --> <property name="build.classpath" refid="classpath"/> <target name="prepare" > <mkdir dir="${build.dir}"/> <mkdir dir="${build.classes.dir}"/> </target> <!-- Now compile --> <target name="compile" depends="prepare"> <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="**"> <classpath refid="classpath"/> </javac> </target> <!-- Create EJB-JAR and place it in deploy directory of JBoss server --> <target name="ejbjar" depends="compile"> <jar jarfile="build/mojo.jar"> <fileset dir="${build.classes.dir}"> <include name="*.class"/> <include name="*.class"/> </fileset> <fileset dir="${src.resources}/"> <include name="**/*.xml"/> </fileset> </jar> <copy file="build/mojo.jar " todir="${jboss.home}/server/default/deploy"/> </target> <!-- Now run the client --> <target name="run.client" depends="ejbjar"> <java classname="Client" fork="yes" dir="."> <classpath refid="classpath"/> </java> </target> <!-- Clean the database - only when JBoss is not running --> <target name="clean.db"> <delete dir="${jboss.home}/server/default/db/hypersonic"/> </target> <!-- Clean up and undeploy --> <target name="clean"> <delete dir="${build.dir}"/> <delete file="${jboss.home}/server/default/deploy/mojo.jar"/> </target> </project>
--------------------------------------------------------------------------