Before you can start coding real J2EE applications, you need a J2EE implementation and a Java development environment, such as Sun Microsystems JDK or a Java Integrated Development Environment (IDE). This book uses the Sun Microsystems' J2EE SDK, which is a complete reference implementation of J2EE. It includes all the classes, containers, and tools you need to learn J2EE.
To run the example code provided on the CD-ROM accompanying this book, you will also need to install a sample database. Installing the sample database is described in the Exercise at the end of this day's lesson. But now, to give you some hands-on work before you study the theory behind J2EE, you will install the J2EE SDK 1.3 on your workstation.
Note
The J2EE SDK is free to download, use for learning J2EE, and use as a development tool. The Sun Microsystems license for this product states that you may not use it in a production environment. Be warned!
Before you download the SDK, ensure that you have J2SE 1.3.1_01 (also known as JDK 1.3.1) or later correctly installed and are using one of the following supported platforms:
Windows NT4 or 2000
Solaris SPARC 7 or 8
Linux Redhat v 6.0 or 6.1
You can use a Java IDE that supports J2SE 1.3 (or later) in preference to the Sun Microsystem's J2SE JDK 1.3.1.
Before installing J2EE SDK 1.3, you must uninstall any previous versions of the J2EE SDK.
Finally, you must ensure that you have a JAVA_HOME environment variable that points to the location of the directory where you installed J2SE SDK (or your preferred Java IDE). This should have been defined when you (or your administrator) installed the J2SE SDK (JDK). If the JAVA_HOME variable is not defined, you must define it now as follows.
If you are using Windows NT or 2000 (remember J2EE SDK is not supported on other Windows' platforms) you should set the JAVA_HOME variable in your system environment so that it is defined for all of the programs you run. Do this using the Control Panel as follows:
1. |
Within the Control Panel, select System. |
2. |
The System Properties dialog appears, select the Advanced tab. |
3. |
Click Environment Variables. |
4. | |
5. | |
6. |
Click OK to clear each of the dialogs. You must have administrator privileges to edit or change system environment variables. If you do not have administrator privilege, you can still use the JDK, but you'll have to define the variables in your user environment. Any other users of your workstation will also have to define the same variables in their environment. |
If you are using Linux or Unix and the JAVA_HOME environment variable does not exist, you can set it with the following command(you must be using the Bourne, Korn, Bash or compatible shell):
JAVA_HOME=/usr/local/jdk1.3.1 export JAVA_HOME
This example assumes you have installed the Sun Microsystems' JDK 1.3.1 in /usr/local.
Typically, you will add these variable definitions to your login environment by adding the same two lines to the.profile file in your home directory.
Finally, you should ensure that the JDK bin directory is in your search path (again this should already be configured on your workstation).
For Windows users, if your search path does not contain the JDK bin directory, add the following directory to your PATH via the Control Panel:
%JAVA_HOME%in
For Linux/Unix users, if your search path does not contain the JDK bin directory, add the following line to your .profile file:
PATH=$PATH:$JAVA_HOME/bin
Now download the J2EE SDK in the format appropriate to your system from http://java.sun.com/j2ee/sdk_1.3/index.html. You should download the J2EE SDK to a temporary directory because the installation process will ask you where to install the SDK.
The installation of the SDK is quite straightforward; just follow the instructions for your platform:
Windows— Double-click the icon of the j2sdkee-1_3_01-win.exe file and follow the onscreen instructions.
Solaris— Issue the following commands to make the download bundle executable and run the installation::
chmod a+x ./j2sdkee-1_3__01-solsparc.sh ./j2sdkee-1_3__01-solsparc.sh
Linux— Change directories to the required parent directory for the J2EE SDK (for example, /usr/local) and extract the download bundle using the following command:
tar – xzvf <download_directory>/j2sdkee-1_3_01-linux.tar.gz
Now you must:
Define the J2EE_HOME variable.
Add the J2EE classes to your CLASSPATH.
You have already been shown how to define variables and change your path for your environment (Windows users use the Control Panel and Linux/Unix users add variable definition lines to .profile), so making these changes will be straightforward.
Windows users must (assuming the J2EE SDK was installed on the C: drive)
Define the following environment variable
J2EE_HOME=c:j2eesdk1.3
Linux/Unix users must add the following to their .profile (assuming the J2EE SDK was installed in /usr/local/j2eesdk1.3):
Define the following environment variable:
J2EE_HOME=/usr/local/j2eesdk1.3
Update the PATH variable for J2EE SDK:
PATH=$PATH:$J2EE_HOME/bin
Add the J2EE JAR files to the CLASSPATH variable:
CLASSPATH=$CLASSPATH:$J2EE_HOME/lib/j2ee.jar:$J2EE_HOME/lib/locale
If your CLASSPATH variable is not currently defined, you must ensure that it includes the current directory so the full setting will be:
CLASSPATH=.:$J2EE_HOME/lib/j2ee.jar:$J2EE_HOME/lib/locale
That is it! You are now ready to start using the J2EE SDK.
Note
All the documentation for the J2EE utility programs and class files is contained in the J2EE SDK download bundle. You will find the documentation in the docs sub-directory of the J2EE installation directory.
The J2EE SDK includes a Reference Implementation of J2EE. This Reference Implementation (RI) contains the following software programs:
A J2EE server
A relational database called Cloudscape
An HTTP (Web) server
A JNDI service implementation
A JMS service implementation
Class files for the J2EE APIs
Various administration and support utilities
The server software components of the RI (database, JNDI, Web server, and J2EE server) are purely for development and are not designed for commercial use. The J2EE RI has been used for the code shown in this book because it is free of charge and conforms to the J2EE 1.3 specification.
To develop J2EE applications and run the example code presented in this book, you will need to start the J2EE server and the Cloudscape database server. Starting the J2EE server will also start the JNDI, JMS, and HTTP servers provided with the J2EE RI.
There are no graphic tools for managing the J2EE and Cloudscape servers, so you will have to start them from the command line. Each server should be started in a separate command window or Telnet window if you are not using a graphical console. It does not matter in which order you start the J2EE and Cloudscape servers.
In the following examples, you must have configured your search path (the PATH environment variable) to include the J2EE SDK bin directory as shown previously.
To start the J2EE RI server, create a new window with access to a command-line prompt (command window for Windows NT/2000 or a terminal or shell window for Linux/Unix users). Enter the following command at the prompt:
j2ee –verbose
This will start the J2EE server in the current window with diagnostic messages displayed in the window. If (or when) you have problems deploying your J2EE applications to the server, it is this window you should examine for error messages. If you put simple diagnostic messages in your EJBs that write to System.out or System.err, these messages will also appear in this window.
Listing 2.1 shows the diagnostic messages issued as the J2EE RI and associated servers startup.
If you start the J2EE server without the -verbose option, all the diagnostic messages will be written to log files in the logs sub-directory of the J2EE SDK installation directory. You will find additional logging information is also written to these log files even with the -verbose option specified.
The J2EE log files are stored in a sub-directory named after your workstation in the logs sub-directory of the J2EE SDK installation directory. Further sub-directories are used to separate the log files for the J2EE, JMS, and HTTP servers.
To start up the Cloudscape database server, you must open a new window and enter the following command:
cloudscape –start
Again, some simple diagnostic messages will be displayed as the server starts up, as shown in Listing 2.2.
You should have no problems starting up either server. If you do have problems, check the error messages displayed in the relevant window. The most likely problems are discussed in the rest of this section.
You will not be able to run J2EE RI and Cloudscape unless you have installed the J2EE SDK in a writeable directory. If you have installed the J2EE SDK as a privileged user (Administrator, root, or whomever), make sure that you grant your normal login account read and write permission to the installation directory and all contained files and directories.
Although the J2EE SDK software uses TCP port numbers that are not normally used by other software, there is always a possibility that there will be a port number conflict.
If a port is used by another software server, a J2EE component will fail to start up and you will see an error message stating that the server “Could not connect to a required port.” The error will normally include a stack trace.
The most likely cause of a port conflict is where you (or another developer) have already started the J2EE server. Listing 2.3 shows the error message for this situation.
You cannot run more than one J2EE RI server on the same workstation.
If your port conflict is with another piece of software, try to disable this software when running J2EE RI and Cloudscape. If this is not possible, you can change the port numbers used by each J2EE server by editing the file called server.xml in the conf sub-directory of the J2EE SDK installation directory. The definitions of the default server port numbers are obvious if you are used to reading and editing XML files. Changing the J2EE RI port numbers is something you should avoid if at all possible.
A common error when working with the J2EE RI is to forget to start up the Cloudscape database server in a separate window. If you fail to start the database and use a J2EE component, such as an EJB, that accesses the database, you will get the following error:
java.sql.SQLException: Connection refused to host: <hostname>; nested exception is:
java.net.ConnectException: Connection Refused: no further information
To solve this problem, start up the Cloudscape server as described previously and stick a note to your monitor to remind you to start the database as well as J2EE. If you are confident with batch or shell scripts, you can write your own scripts to start both servers in separate windows.
The following simple scripts will work for the indicated platforms:
On Windows NT/2000, use
start "J2EE" j2ee –verbose start "Cloudscape" cloudscape –start
On Solaris, use
dtterm –name j2ee –e "j2ee –verbose" & dtterm –name cloudscape –e "cloudscape –start" &
On Linux, use
To close down J2EE RI and Cloudscape, you should use the following commands:
j2ee –stop cloudscape –stop
These commands can be run from any command window. Remember that the J2EE and Cloudscape server windows are busy and cannot accept additional commands while the servers are running.
Although Sun Microsystems do not recommend this approach, typing ^C (Ctrl+C) in the server window or simply closing the window will also shut down the servers.
A Java IDE (JDK) and a J2EE implementation (J2EE SDK) are all you require to learn how to develop J2EE applications. However, areas of this book look at using J2EE applications in a wider context and make use of additional (freely available) software.
So that you are aware of this software, Table 2.1 lists the optional software used in this book. Full instructions for downloading and configuring this software (should you want to do so) are included in the relevant day's instructions. You do not need to download this software at the present time.
Day | Software | Resource URL |
---|---|---|
3 | OpenLDAP and a Unix system to run the Open LDAP server. | http://www.openldap.org/software/download/ |
14 | JSPTL Java Standard Tag libraries from the Apache Jakarta project. | http://jakarta.apache.org/taglibs/index.html |
17 | XALAN from the Apache project. | http://xml.apache.org/xalan-j/index.html |
20 | Apache Axis alpha2. | http://xml.apache.org/axis/index.html |
Apache Tomcat 4.0.1. | http://jakarta.apache.org/tomcat/index.html | |
JAXM 1.0 reference implementation (part of the “JAX Pack Fall 01”). | http://java.sun.com/xml |
52.15.42.128