天天看點

Setting Up a Java 2 Environment on Linux

<b>Setting Up a Java 2 Environment on Linux</b>

<b>J2SDK</b>

The first thing you'll need to do is download the Java 2 SDK for Linux from [url]http://java.sun.com/j2se/downloads.html/[/url]. The latest version as of this writing is J2SE 1.4.2 (Java 2 SDK, Standard Edition 1.4.2_03). You can either use a self-extracting file or a RPM (from a self-extracting file). Download the SDK, not the JRE.

The "RPM in self-extracting file" will install Java in /usr/java by default. The "self-extracting file" will install Java in whatever directory you run the file from.

Linux RPM in self-extracting file

Download and copy j2sdk-1_4_2_03-linux-i586-rpm.bin to $HOME/src (or a temporary location of your choice).

% su -

# cd $HOME/src

# chmod +x j2sdk-1_4_2_03-linux-i586-rpm.bin

# ./j2sdk-1_4_2_03-linux-i586-rpm.bin

# rpm -ivh j2sdk-1_4_3_03-linux-i586.rpm

Now test Java:

# /usr/java/j2sdk1.4.2_03/bin/java -version

Set the PATH environment variable: (/etc/profile or .bashrc).

PATH="$PATH:/usr/java/j2sdk1.4.2_03/bin:."

export PATH

Linux self-extracting file

Download and copy j2sdk-1_4_2_03-linux-i586.bin to $HOME/src (or a location of your choice).

# mkdir /usr/java (if it doesn't exist)

# mv $HOME/src/j2sdk-1_4_2_03-linux-i586.bin /usr/java

# cd /usr/java

# chmod +x j2sdk-1_4_2_03-linux-i586.bin

# ./j2sdk-1_4_2_03-linux-i586.bin

<b>J2EE SDK</b>

Next you'll need to install the Java 2 SDK Enterprise Edition from [url]http://java.sun.com/j2ee/download.html[/url]. The latest version as of this writing is J2EE 1.3.1 (Java 2 Platform, Enterprise Edition). Download and copy jsdkee-1_3_1-linux.tar.gz to /usr/local.

# cd /usr/local

# gzip -d j2sdkee-1_3_1-linux.tar.gz

# tar xvf j2sdkee-1_3_1-linux.tar

# cd j2sdkee1.3.1/bin

# vi userconfig.sh

Set J2EE_CLASSPATH and JAVA_HOME

The Java 2 SDK, Enterprise Edition relies on the environment variables that are set in the user configuration script. This script is in the userconfig.sh file of the bin directory of your installation.

The userconfig.sh file sets these environment variables:

* J2EE_CLASSPATH - the classpath referenced by the J2EE server. J2EE_CLASSPATH must include the location of JDBC driver classes (except for those used by the Cloudscape DBMS included in this download bundle). Each location is delimited by a colon. J2EE_CLASSPATH does not need to include the Java 2 SDK, Enterprise Edition classes (j2ee-jar); the Java 2 SDK, Standard Edition software; or the classes contained in the enterprise application.

* JAVA_HOME - the absolute path of the directory in which the Java 2 SDK, Standard Edition is installed.

Here's an example of how you might edit the userconfig.sh file:

J2EE_CLASSPATH=/opt/oracle/jdbc:/usr/local/db/driver.zip

export J2EE_CLASSPATH

or

#J2EE_CLASSPATH=

#export J2EE_CLASSPATH

JAVA_HOME="/usr/local/j2sdk1.4.1_02"

export JAVA_HOME

JAVA_HOME="/usr/java/j2sdk1.4.1_02"

<b>Ant</b>

NOTE: Ant comes with Xerces 2.

Download Ant from ant.apache.org. The latest version as of this writing is 1.5.3. Copy the file apache-ant-1.5.3.tar.gz to /usr/local.

# tar zxvf apache-ant-1.5.3.tar.gz

Documentation - /usr/local/apache-ant-1.5.3/docs/manual/index.html

Now you can refer to your install directory /usr/local/apache-ant-1.5.3 as ANT_HOME.

Create ANT_HOME Environment variable

# vi /etc/profile

ANT_HOME="/usr/local/apache-ant-1.5.3"

export ANT_HOME

Put Ant executable in PATH

PATH="$PATH:$ANT_HOME/bin"

Xerces

Xerces is an XML parser available from the Apache Software Foundation. Download Xerces from xml.apache.org/dist/xerces-j/.

Version 2

The latest version as of this writing is Xerces-J-bin.2.4.0.tar.gz. There is a source version available, but this install uses the binary. Download and copy Xerces-J-bin.2.4.0.tar.gz to /usr/local.

# gzip -d Xerces-J-bin.2.4.0.tar.gz

# tar xvf Xerces-J-bin.2.4.0.tar

If you download the .zip file, you can do this:

# jar xf Xerces-J-bin.2.4.0.zip

Set CLASSPATH

export CLASSPATH="$CLASSPATH:/usr/local/xerces-2_4_0/xercesImpl.jar:/usr/local/xerces-2_4_0/xmlParserAPIs.jar"

See the docs at /usr/local/xerces-2_4_0/docs/install.html for more info.

Version 1

The latest version as of this writing is Xerces-J-bin.1.4.4.tar.gz. There is a source version available, but this install uses the binary. Download and copy Xerces-J-bin.1.4.4.tar.gz to /usr/local.

# gzip -d Xerces-J-bin.1.4.4.tar.gz

# tar xvf Xerces-J-bin.1.4.4.tar

# jar xf Xerces-J-bin.1.4.4.zip

export CLASSPATH="$CLASSPATH:/usr/local/xerces-1_4_4/xerces.jar"

<b>Tomcat</b>

Get the Tomcat binary from [url]http://jakarta.apache.org/downloads/binindex.html[/url]. For this install, I used jakarta-tomcat-4.1.18.tar.gz.

Copy file to /usr/local.

# cd /usr/local/

# gzip -d jakarta-tomcat-4.1.18.tar.gz

# tar xvf jakarta-tomcat-4.1.18.tar.gz

You may want to set up a symbolic link to /usr/local/jakarta-tomcat... Set the CATALINA_HOME environment variable. I set this in /etc/profile:

CATALINA_HOME="/usr/local/jakarta-tomcat-4.1.18"

export CATALINA_HOME

Set the JAVA_HOME environment variable. I set this in /etc/profile:

Starting Tomcat

# cd /usr/local/jakarta-tomcat-4.1.18/bin

# ./startup.sh

Stoping Tomcat

# ./shutdown.sh

Testing Tomcat

Point your browser to

localhost:8080

and you should see the default Tomcat page. This page includes JSP and Servlet examples for you to try.

Create a New Application

The Easy Way

First we need to edit the server.xml file located at $TOMCAT_HOME/conf/server.xml. We need to add a new Context configuration. I'm going to add a Context named "sparling":

&lt;Context path="/sparling"

docBase="webapps/sparling"

crossContext="false"

reloadable="true"&gt;

&lt;/Context&gt;

Refer to the User's Guide for an explantation of these lines.

Now we need to create the sparling directory to $TOMCAT_HOME/webapps.

# cd $CATALINA_HOME

# cd webapps

# mkdir sparling

# cd sparling

# mkdir WEB-INF

# cd WEB-INF

# mkdir classes

Now restart Tomcat. This will rebuild the tomcat-apache.conf file.

Now move your servlet class file to the $CATALINA_HOME/webapps/sparling/WEB-INF/classes directory. For example, if we add the file HelloWorld.class to this directory, then we can access the servlet via the browser at: [url]http://localhost:8080/sparling/servlet/HelloWorld[/url].

Additional Info

* Tomcat-A Minimalistic User's Guide

* Linux as an Application Server-The Tomcat Way, Chris Bush, SysAdmin, January 2001

* Server-Side Java with Jakarata-Tomcat, Reuven M. Lerner, Linux Journal, April 2001

* JavaServer Pages, Reuven M. Lerner, Linux Journal, May, 2001

* The Apache XML Project: How to Get Read All Over, Rick Wayne, Software Development, June 2001

Running Tomcat as non-root user

Create tomcat user

# chown -R tomcat /usr/local/jakarta-tomcat-4.1.18

Create/edit /etc/rc.d/init.d/tomcat

#!/bin/sh

#

# Startup script for Tomcat

JAVA_HOME=/usr/java/j2sdk1.4.1_02

start_tomcat=/usr/local/jakarta-tomcat-4.1.18/bin/startup.sh

stop_tomcat=/usr/local/jakarta-tomcat-4.1.18/bin/shutdown.sh

start() {

echo -n "Starting tomcat: "

su -c ${start_tomcat} - tomcat

echo "done."

}

stop() {

#echo -n "Shutting down http: "

echo -n "Shutting down tomcat: "

${stop_tomcat}

# See how we were called

case "$1" in

start)

start

;;

stop)

stop

restart)

sleep 10

*)

echo "Usage: $0 {start|stop|restart}"

esac

exit 0

Compiling Servlets

export CLASSPATH=$CLASSPATH:$CATALINA_HOME/common/lib/servlet.jar

Jakarta Struts

[url]http://jakarta.apache.org/builds/jak...elease/v1.0.2/[/url] [url]http://www.apache.org/dist/jakarta/struts/binaries/[/url]

Cocoon

Go to [url]http://xml.apache.org/cocoon/dist/[/url] and download cocoon. Version 2.04 is the latest version at this writing. You will have to select a distribution for eiher Java 1.3/1.2 or Java 1.4. I downloaded cocoon-2.0.4-vm14-bin.tar.gz. The download location is not imporatant, as you will just need to move a .war file to Tomcat. I use /usr/local/src.

# cd /usr/local/src

# tar zxvf cocoon-2.0.4-vm14-bin.tar.gz

# cd cocoon-2.0.4

# cp cocoon.war /usr/local/jakarta-tomcat-4.1.18/webapps/

# /etc/rc.d/init.d/tomcat stop

# /etc/rc.d/init.d/tomcat start

it s from

[url]http://www.dougsparling.com/comp/howto/linux_java.html[/url]