Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Enterprise Java for Linux HOWTO

How to set up an Enterprise Java environment on Linux including a Java
Development Kit, a Web server, supporting Java servlets, accessing a
database via JDBC, and supporting Enterprise Java Beans (EJBs).
______________________________________________________________________

Table of Contents



1. Introduction

1.1 Background
1.2 Audience
1.3 New Versions
1.4 Copyright and License
1.5 Disclaimers
1.6 Potential Future Sections
1.7 Other Resources
1.8 Feedback

2. How to Setup the Java Development Kit

2.1 Blackdown JDK
2.1.1 Background
2.1.2 Download
2.1.3 Installation
2.1.4 Setting up Your Environment
2.1.5 Confirming Your Installation
2.1.6 More Information
2.2 IBM Java Developer Kit
2.2.1 Background
2.2.2 Download
2.2.3 Installation
2.2.4 Setting up Your Environment
2.2.5 Confirming Your Installation
2.2.6 More Information
2.3 Kaffe
2.3.1 Background
2.3.2 Download and Installation
2.3.3 Setting up Your Environment
2.3.4 Confirming Your Installation
2.3.5 More Information
2.4 Sun J2SE
2.4.1 Background
2.4.2 Download
2.4.3 Installation
2.4.4 Setting up Your Environment
2.4.5 Confirming Your Installation
2.4.6 More Information

3. How to Setup the Web Server

3.1 Apache
3.1.1 Background
3.1.2 Download, Installation, and Setting up Your Environment
3.1.3 Confirming Your Installation
3.2 IBM Domino
3.3 IBM HTTP Server
3.3.1 Background
3.3.2 Download
3.3.3 Installation
3.3.4 Setting up Your Environment
3.3.5 Confirming Your Installation
3.3.6 More Information
3.4 Jetty HTTP Server and Servlet Container
3.4.1 Background
3.4.2 Download
3.4.3 Installation

4. How to Setup Java Servlet Support

4.1 Allaire JRun
4.2 Apache Tomcat
4.2.1 Background
4.2.2 Download
4.2.3 Installation
4.2.4 Setting up Your Environment
4.2.5 Confirming Your Installation
4.2.6 More Information
4.3 BEA WebLogic
4.4 Enhydra
4.5 IBM WebSphere
4.6 Locomotive
4.7 Jetty

5. How to Setup Java Server Pages (JSP) Support

5.1 Apache Jakarta
5.2 Caucho Resin
5.3 Jetty

6. How to Setup JDBC Support

6.1 IBM DB2
6.2 MiniSQL
6.3 MySQL
6.4 Oracle
6.5 PostgreSQL
6.5.1 Background
6.5.2 Download and Installation
6.5.3 Confirming Your Installation
6.5.4 More Information
6.6 Sybase
6.6.1 Background
6.6.2 Download
6.6.3 Installation
6.6.4 Confirming Your Installation
6.6.5 More Information

7. How to Setup Enterprise Java Bean (EJB) Support

7.1 BEA WebLogic
7.2 EJBoss
7.2.1 Background
7.2.2 Download
7.2.3 Installation
7.2.4 Setting up Your Environment
7.2.5 Confiming Your Installation
7.3 Bullsoft JOnAS EJB


______________________________________________________________________

1. Introduction



1.1. Background

This document was started January, 1999 by Gary Meyer (
gary@meyer.net) after several weeks of installing various open source
and proprietary Enterprise Java products for Linux. "Enterprise Java"
is defined as using the Java Enterprise APIs.

Some updates were added by Greg Wilkins (gregw@mortbay.org) in
November 2001 however parts of the document are still out of date.



1.2. Audience

This HOWTO is intended to benefit software professionals who are
interested in evaluating, developing, or deploying Enterprise Java on
Linux. Limited knowledge or experience in either Linux or Java is
assumed.


1.3. New Versions

The newest version of this document can be found at the Linux
Documentation Project website at:
http://linuxdoc.org/HOWTO/Enterprise-Java-for-Linux-HOWTO.html



1.4. Copyright and License

This document is Copyright (c) 1999-2001 by Gary Meyer and Greg
Wilkins. Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Free Documentation License,
Version 1.1 or any later version published by the Free Software
Foundation; with no Invariant Sections, with no Front-Cover Texts, and
with no Back-Cover Texts. A copy of the license is available at
http://www.gnu.org/copyleft/fdl.html


1.5. Disclaimers


The suggestions in this document are provided to help you get a
Enterprise Java environment on Linux up and running as quickly as
possible. The suggestions are not product recommendations or
endorsements. As you become familiar with the options available, you
can do you own product evaluations and determine what options are best
for your particular purpose.

For the purpose of this HOWTO, "Enterprise Java" is defined as using
the Java Enterprise APIs. This HOWTO does not address scalability,
availability, manageability, and other such aspects of software that
are often associated with the word "enterprise."

1.6. Potential Future Sections


This HOW has focused on the most popular aspects of Enterprise Java.
The following sections may be added to this HOWTO.

· Integrated Development Environments (IDEs) that Support Enterprise
Java for Linux

· Java Naming and Directory Interface (JNDI) Support

· Java Mail API (JMAPI) Support

· Java Transaction Service (JTS) Support

· Java Interface Definition Language (JIDL) Support

· Java Messaging Service (JMS) Support

· Common Object Request Broker Architecture (CORBA) Support

Interested in authoring a section?


Please contact the author, Gary Meyer, at ( gary@meyer.net).

1.7. Other Resources


The App-Serv Center website at http://www.app-serv.com/.

Java Enterprise in a Nutshell by David Flanagan et al at
http://www.oreilly.com/catalog/jentnut/.

1.8. Feedback


Please submit all additions and corrections to the author, Gary Meyer,
at (gary@meyer.net).

2. How to Setup the Java Development Kit


There are several Java Development Kits available for Linux. These
include:


· ``Blackdown JDK''

· ``IBM Java Developer Kit ''

· ``Sun J2SE ''

· ``Kaffe ''

If you are going to try just one JDK, I suggest you initially try the
Sun J2SE, unless you are recommended otherwise by specific software
you are using or want to use. Additionally, if you are interested in
an open source implementation, you will need to use Kaffe.

2.1. Blackdown JDK


2.1.1. Background


The Blackdown JDK is a port of Sun JDK to Linux. As of the time of
this writing, the Blackdown JDK is current with JDK 1.2.2 on the Intel
architecture and 1.1.8 on the PowerPC.

In December 1999, Sun announced availability of the Java 2 Platform,
Standard Endition (J2SE) on Linux. This Sun release has significant
impact on Blackdown because Blackdown is a port. In a press release,
Sun states, "This week's announcement would not have been possible
without the collaboration of Blackdown, a group of developers and
enthusiasts around the globe. Since its inception, Blackdown has been
a provider of Java technology for the Linux platform. Their dedicated
effort over a number of years has laid the foundation for this release
of the Java 2 platform port to Linux; in particular their effort was
critical to the success of this release."

Additionally, the Sun press release continues, "Blackdown.org
continues to be a valuable source for Java technology for Linux,
including JDK 1.1.x releases."

2.1.2. Download


The Blackdown JDK can be obtained from http://www.blackdown.org.

>From the Blackdown home page, select download and a mirror site.

Select the version of the JDK you want. If other software that you
are wanting to use does not recommend a specific version, I suggest
the most current, which is at the time of this writing, JDK 1.2.2.

Select the machine architecture you are installing on. For Intel
architecture, select i386.

Select the release candidate you want. If other software that you are
wanting to use does not recommend a specific release candidate, I
suggest the most recent or final version if available.

For the Blackdown JDK, there are possibly a number of different files
available in different packaging formats. Additionally you have to be
sure you get support for the right libc for your Linux distribution.

The files available include:

· jdk - The Java Development Kit contains everything you need to
compile, run, and debug Java. It does not contain international
character converters.


· jre - The Java Runtime Environment, including international
character converters.


· rt - A minimal Java Runtime Environment that does not include
international character converters.

· i18n - The internationalization font mappings and a JAR containing
the international character converters.

· native - Additional binaries providing native thread support.

I suggest downloading only the jdk for Java development in English.

When downloading the Blackdown files, you may need to select between
libc5 and glibc as well as potentially a specific version of glibc.
The libc options include:

· libc5 - The older, and still most common, Linux libc is libc5.

· glibc - The new Linux libc.

If you are using a newer distribution of Linux, you will most likely
have glibc. I suggest initially trying glibc.

2.1.3. Installation


I suggest installing files in the /usr/local directory. After
downloading the files, run:

mkdir /usr/local/blackdown
mv jdk* /usr/local/blackdown



If you downloaded the tarball format, run:

tar zxvf [filename].tar.gz



Where [filename] is the name of the file.

Under the /usr/local/blackdown directory, you now should see a
directory such as jdk1.2.2.

The above example shows JDK 1.2.2 release candidate 3 for the Intel
architecture. Substitute the file name, version number, release
candidate number, and architecture as appropriate. You will need to
open each distribution package file in the above manner.

2.1.4. Setting up Your Environment


The environment variables to set up are:

· JAVA_HOME

· PATH

· CLASSPATH

The JAVA_HOME environment variable references the home directory of
your JDK installation. Set your JAVA_HOME environment variable to the
directory into which you just installed a version of the Blackdown
JDK.

export JAVA_HOME=/usr/local/blackdown/jdk1.2.2



The $JAVA_HOME/bin directory has the Java compiler (javac) and the
Java Virtual Machine (java) as well as other necessary programs for
development. Add $JAVA_HOME/bin to your PATH.

export PATH=$JAVA_HOME/bin:$PATH



Note that $JAVA_HOME/bin was added to the front of the PATH so that
the installed JDK will be used rather than any JDK that might have
come with your Linux distribution.

To confirm that your PATH is correctly set up, check which Java
compiler and JVM will be used.

which javac
which java



The output should reference javac and java in your $JAVA_HOME/bin
directory.

The CLASSPATH environment variable references all JARs and directories
that you will need to compile and run Java programs.

For JDK 1.2.2, you don't need to initially add any JARs to your
CLASSPATH. JARs can be packaged in either .jar or .zip files.

export CLASSPATH=$CLASSPATH:.



2.1.5. Confirming Your Installation


You are now ready to compile and run a simple application. Create the
following program.

class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello, World!");
}
}



Compile the program with the Java compiler.

javac HelloWorld.java



If the compiler produces errors, double check the syntax and confirm
your PATH and CLASSPATH.

Run the program with the JVM.

java HelloWorld



If the JVM produces errors, confirm your PATH and CLASSPATH.

You should see the following output:

Hello, World!



Congratulations, you have installed, set up an environment for, and
tested the Blackdown JDK on Linux.

2.1.6. More Information


For more information on the Blackdown JDK, see the Blackdown website
at http://www.blackdown.org. There is an excellent FAQ available.

2.2. IBM Java Developer Kit


2.2.1. Background


The IBM Java Developer Kit and Runtime Environment pass Sun's Java
compatibility test and include the latest maintenance. (From the IBM
website.)

As of the time of this writing, the IBM Java Developer Kit is current
with JDK 1.1.8 and is available only on the Intel architecture.

2.2.2. Download


The IBM Java Developer Kit can be obtained from
http://www.ibm.com/java/jdk/118/linux.


In order to download, you will have to register with the IBM website
and agree to the license online.

The files available include:

· ibm-jdk-l118-linux-x86.tgz - The Java Development Kit contains
everything you need to compile, run, and debug Java.

· ibm-jre-l118-linux-x86.tgz - The Java Runtime Environment contains
everything you need to run Java.

Since you will be doing Java development, I suggest downloading the
ibm-jdk tarball file.

2.2.3. Installation


I suggest installing files in the /usr/local directory. After
downloading the files, run:

mkdir /usr/local/ibm
mv ibm-jdk-l118-linux-x86.tgz /usr/local/ibm



You can now open the distribution package. To do this, type:

tar zxvf ibm-jdk-l118-linux-x86.tgz



Under the /usr/local/ibm directory, you now should see the jdk118
directory.


The above example shows JDK 1.1.8 for the Intel architecture.
Substitute the filenames as appropriate.

2.2.4. Setting up Your Environment


The environment variables to set up are:

· JAVA_HOME

· PATH

· CLASSPATH

The JAVA_HOME environment variable references the home directory of
your JDK installation. Set your JAVA_HOME environment variable to the
directory into which you just installed a version of the IBM Java
Developer Kit.

export JAVA_HOME=/usr/local/ibm/jdk118



The $JAVA_HOME/bin directory has the Java compiler (javac) and the
Java Virtual Machine (java) as well as other necessary programs for
development. Add $JAVA_HOME/bin to your PATH.

export PATH=$JAVA_HOME/bin:$PATH



Note that $JAVA_HOME/bin was added to the front of the PATH so that
the installed JDK will be used rather than any JDK that might have
come with your Linux distribution.

To confirm that your PATH is correctly set up, check which Java
compiler and JVM will be used.

which javac
which java



The output should reference javac and java in your $JAVA_HOME/bin
directory.

The CLASSPATH environment variable references all JARs and directories
that you will need to compile and run Java programs.

Initially I suggest adding the following JARs to your CLASSPATH. JARs
can be packaged in either .jar or .zip files.

For instance:

export CLASSPATH=$JAVA_HOME/lib/classes.zip
export CLASSPATH=$CLASSPATH:.



2.2.5. Confirming Your Installation


You are now ready to compile and run a simple application. Create the
following program.

class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello, World!");
}
}



Compile the program with the Java compiler.

javac HelloWorld.java



If the compiler produces errors, double check the syntax and confirm
your PATH and CLASSPATH.

Run the program with the JVM.

java HelloWorld



If the JVM produces errors, confirm your PATH and CLASSPATH.

You should see the following output:

Hello, World!



Congratulations, you have installed, set up an environment for, and
tested the IBM Java Developer Kit on Linux.

2.2.6. More Information


For more information on the IBM Java Developer Kit, see the IBM Java
website at http://www.ibm.com/java.


2.3. Kaffe


2.3.1. Background


Kaffe is a cleanroom, open source implmentation of a Java Virtual
Machine and class libraries. As of the time of this writing, Kaffe
"mostly complies with JDK 1.1, except for a few missing parts." And
"parts of it are already JDK 1.2 (Java 2) compatible." (From the
Kaffe website.)


Kaffe may have already been shipped with your Linux distribution
because of its open source license.

2.3.2. Download and Installation


Rather than downloading from Kaffe, I suggest you initially try the
Kaffe that most likely came with your Linux distribution.

Alternatively, Kaffe can be obtained from http://www.kaffe.org.

>From the Kaffe home page, select the current release. At the time of
this writing, the current release is 1.0.5. The Kaffe version number
has no relationship to JDK specification version numbers.

2.3.3. Setting up Your Environment


The environment variables to set up are:

· PATH

· CLASSPATH

To confirm that your PATH is correctly set up, check which Java
compiler and JVM will be used.

which javac
which java



The CLASSPATH environment variable references all JARs and directories
that you will need to compile and run Java programs.

Initially I suggest you add the following JARs to your CLASSPATH. JARs
can be packaged in either .jar or .zip files.

For instance:

export CLASSPATH=/usr/local/share/kaffe/Klasses.zip
export CLASSPATH=$CLASSPATH:.

2.3.4. Confirming Your Installation


You are now ready to compile and run a simple application. Create the
following program.

class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello, World!");
}
}



Compile the program with the Java compiler.

javac HelloWorld.java



If the compiler produces errors, double check the syntax and confirm
your PATH and CLASSPATH.

Run the program with the JVM.

java HelloWorld



If the JVM produces errors, confirm your PATH and CLASSPATH.

You should see the following output:

Hello, World!



Congratulations, you have installed, set up an environment for, and
tested Kaffe on Linux.

2.3.5. More Information


For more information on Kaffe, see the Kaffe website at
http://www.kaffe.org.

2.4. Sun J2SE


2.4.1. Background


The Sun Java 2, Standard Edition (J2SE) is Sun's production release of
the Java 2 Platform for the Linux operating system. As of the time of
this writing, J2SE is current with JDK 1.2.2 on the Intel
architecture.

2.4.2. Download


J2SE can be obtained from
http://developer.java.sun.com/developer/earlyAccess/j2sdk122.


You will need to register with Sun and agree to the license online
before downloading.
2.4.3. Installation


I suggest installing files in the /usr/local directory. After
downloading the files, run:

mkdir /usr/local/sun
mv jdk1_2_2rc1-linux-i386.tar.gz /usr/local/sun



You can now open the distribution package. To do this, type:

tar zxvf jdk1_2_2rc1-linux-i386.tar.gz



Under the /usr/local/sun directory, you now should see the jdk1.2.2
directory.


The above example shows JDK 1.2.2 release candidate 1 for the Intel
architecture. Substitute the filenames as appropriate.

2.4.4. Setting up Your Environment


The environment variables to set up are:

· JAVA_HOME

· PATH

· CLASSPATH

The JAVA_HOME environment variable references the home directory of
your JDK installation. Set your JAVA_HOME environment variable to the
directory into which you just installed a version of J2SE.

export JAVA_HOME=/usr/local/sun/jdk1.2.2



The $JAVA_HOME/bin directory has the Java compiler (javac) and the
Java Virtual Machine (java) as well as other necessary programs for
development. Add $JAVA_HOME/bin to your PATH.

export PATH=$JAVA_HOME/bin:$PATH



Note that $JAVA_HOME/bin was added to the front of the PATH so that
the installed JDK will be used rather than any JDK that might have
come with your Linux distribution.

To confirm that your PATH is correctly set up, check which Java
compiler and JVM will be used.

which javac
which java



The output should reference javac and java in your $JAVA_HOME/bin
directory.

The CLASSPATH environment variable references all JARs and directories
that you will need to compile and run Java programs.

For JDK 1.2.2, you don't need to initially add any JARs to your
CLASSPATH. JARs can be packaged in either .jar or .zip files.

export CLASSPATH=$CLASSPATH:.



2.4.5. Confirming Your Installation


You are now ready to compile and run a simple application. Create the
following program.

class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello, World!");
}
}



Compile the program with the Java compiler.

javac HelloWorld.java



If the compiler produces errors, double check the syntax and confirm
your PATH and CLASSPATH.

Run the program with the JVM.

java HelloWorld



If the JVM produces errors, confirm your PATH and CLASSPATH.

You should see the following output:

Hello, World!



Congratulations, you have installed, set up an environment for, and
tested the Sun J2SE for Linux.

2.4.6. More Information


For more information on Sun J2SE, see the Sun Java website at
http://java.sun.com. There are excellent discussion forums available
where you might be able to find answers to various questions.

3. How to Setup the Web Server


There are several Web Servers available for Linux. These include:

· ``Apache ''

· ``IBM Domino ''

· ``IBM HTTP Server ''

· ``Jetty HTTP Server''

If you are going to try just one Web Server, I suggest you initially
try Apache, principally because it comes with most major Linux
distributions and may already be installed, and perhaps running, on
your system.

3.1. Apache


3.1.1. Background


Apache is the most popular HTTP server on the Internet. It was
originally based upon the NCSA httpd and has since been completely
rewritten. It is Open Source licensed. (From the Apache website.)

3.1.2. Download, Installation, and Setting up Your Environment


Rather than downloading from Apache, I suggest you initially try the
Apache that most likely came with your Linux distribution.

Alternatively, Apache can be obtained from http://www.apache.org.

3.1.3. Confirming Your Installation


To confirm that Apache is installed and running on your computer, open
your web browser, and enter the URL: "http://127.0.0.1". (127.0.0.1
is the IP address for the localhost.)

You should see a web page to the effect of "It Worked!"

If it did not work, you can confirm that Apache is installed by typing
the following on a RedHat Package Manager (RPM)-based Linux
distribution.

rpm -q | grep apache



To start Apache, type:

cd /etc/rc.d/init.d
./httpd start



Note: The httpd script used at boot time may be in a different
location on other Linux distributions.

For more assistance, I suggest you look into the Apache FAQ at
http://www.apache.org/docs/misc/FAQ.html.


3.2. IBM Domino


To be written.

See http://www.lotus.com/dominolinuxfor more information.


3.3. IBM HTTP Server


3.3.1. Background


The IBM HTTP Server is an IBM repackaging of Apache. You might
consider using the IBM HTTP Server if you plan on working with IBM
WebSphere.

At the time of this writing, the most recent version is 1.3.6.1.

3.3.2. Download


The IBM HTTP Server can be obtained from
http://www-4.ibm.com/software/webservers/httpservers/download.html.


Click on the download link and select 56-bit or 128-bit SSL
encryption.


You will need to register with IBM, fill out a marketing survey, and
accept the license agreement before downloading. The IBM HTTP Server
requires glibc either version 2.0 or 2.1. glibc is the new Linux libc.
If you have an older distribution that is based upon libc5, you will
not be able to use the IBM HTTP Server.

On an RedHat Package Manager (RPM)-based Linux distribution, you can
run:


rpm -qa | grep libc



You will see output such as:

glibc-2.1.2-11
libc-5.3.12-31



This will show you which versions of libc5 and glibc you have
installed on your Linux distribution. In my above example I have both
glibc and libc5 installed on my system. glibc is version 2.1, so I
would want to download the files for glibc2.1.

I suggest downloading all of the tar files for the glib version of
Linux that you have as they are relatively small. However, minimally
you will need the server file. For RedHat 6.0 and distributions
derived from Redhat 6.0 you will also need the redhat60only. For
glibc2.0 based distributions you will also need the libstdc file.

3.3.3. Installation


I suggest installing files in the /usr/local directory. After
downloading the files, run:

mkdir /usr/local/ibm
mv HTTPServer.linux.* /usr/local/ibm



You can now open the distribution package file or files.

tar xvf [filename].tar



Where [filename] is the name of the file.

Under the /usr/local/ibm directory, you now should see the directory
IHS.


Use the RedHat Package Manager (RPM) to install the rpm files that the
tar file produced. If you needed the libstdc file, you will need to
install that file first. Then you would install the server RPM file
such as:

cd IHS
rpm -i IBM_HTTP_Server-1.3.6-2.i386.rpm



The above example shows version 1.3.6 for the Intel architecture.
Substitute the filename as appropriate.

After installing you can delete the rpm files as they can be easily
recreated from the tar files.

3.3.4. Setting up Your Environment


The environment variables to set up are:

· PATH

The IBM HTTP Server installed itself into /opt/IBMHTTPServer. You
need to add its bin directory to your PATH.

export PATH=/opt/IBMHTTPServer/bin:$PATH



Note that /opt/IBMHTTPServer/bin was added to the front of the PATH so
that the installed Web Server will be used rather than any Web Server
that might have come with your Linux distribution.

To confirm that your PATH is correctly set up, check which Apache
controller will be used. Type:

which apachectl



The output should reference apachectl in the /opt/IBMHTTPServer/bin
directory.


Note: because the IBM HTTP Server is based upon Apache, it uses the
Apache controller to start and stop it. When you have both the IBM
HTTP Server and Apache installed on a computer, take particular care
to your PATH to make sure you are working with the correct server.

You may need to modify the IBM HTTP Server configuration file. The
configuration file was installed in
/opt/IBMHTTPServer/conf/httpd.conf. The most common two entries that
need to be changed are the ServerName and the Listen port number.
Look for the key words "ServerName" and "Listen" in the httpd.conf
file. The ServerName should be set to either your hostname or IP
address. If your computer uses DHCP to acquire an IP address, the
hostname is a better candidate. However, in order to use your
computer's hostname, your computer's hostname and IP address must be
properly registered in DNS.


Additionally if you are running another Web server on the computer you
need to assign the IBM HTTP Server to another port so you can run both
Web servers simultaneously if necessary.

The following is an example entry in httpd.conf.

ServerName 192.168.0.4
Listen 3000



3.3.5. Confirming Your Installation


To start the IBM HTTP Server, type the following:

/opt/IBMHTTPServer/bin/apachectl start



To confirm that the IBM HTTP Server is installed and running on your
computer, open your web browser, and enter the URL:
"http://192.168.0.4:3000" substituting the correct IP address and port
number entered into httpd.conf.


You should see a web page to the effect of "Welcome to the IBM HTTP
Server". Contratulations, you have installed, set up an environment
for, and tested the IBM HTTP Server for Linux.

3.3.6. More Information


For more information, I suggest you look into the IBM HTTP Server
Support page at
http://www-4.ibm.com/software/webservers/httpservers/support.html.


3.4. Jetty HTTP Server and Servlet Container


3.4.1. Background

Jetty is an Open Source HTTP Servlet Server written in 100% Java. It
is both a full featured HTTP/1.1 server and a Servlet Container. It
is designed to be light weight, high performance, embeddable,
extensible and flexible, thus making it an ideal platform for serving
dynamic HTTP requests from any Java application.

Jetty can be used as a stand-alone HTTP server and servlet container
or it can be embedded in another java application (eg. the JBoss EJB
container is using Jetty as it's prefered server and container
solution).

As a combined server and servlet container, both these functions run
efficiently in a single unix process. Installation and configuration
is also simpler as a single application.

3.4.2. Download

The Jetty HTTP Server and Servlet container may be downloaded via:

http://jetty.mortbay.org.

Jetty is distributed under the artistic license, full source is
included and it can be used and distributed commercially.


3.4.3. Installation

The package is distributed as a gzipped tar file, which can be
unpacked with:

gunzip < Jetty-x.x.x.tgz | tar xf -



Which will create a Jetty-x.x.x directory where x.x.x is the version
number.


To run the demo server:

export JETTY_HOME=
export JAVA_HOME=
$JETTY_HOME/bin/jetty.sh run


Then to see the Jetty demo and tutorial point a browser at
http://localhost:8080.

Jetty can also be installed and run via JMX or as part of the JBoss
distributions. See http://jetty.mortbay.org or the README.TXT file
for more details.


4. How to Setup Java Servlet Support


There are several Web Server plug-ins and Application Servers
available for Linux that provide support for Java Servlets. These
include:

· ``Allaire JRun''

· ``Apache Tomcat''

· ``BEA WebLogic''

· ``Enhydra''

· ``Locomotive''

· ``IBM Websphere ''

· ``Jetty''


4.1. Allaire JRun


To be written.


See http://www.allaire.com/products/jrun/for more information.

4.2. Apache Tomcat


4.2.1. Background

JServ has been replaced with Tomcat from the Apache Jakarta project:
http://jakarta.apache.org/. This section is still written for JServ
and needs to be updated.

Apache JServe is a 100% pure Java servlet engine fully compliant with
the Java Servlet 2.0 specification. Apache JServ is part of the Java
Apache Project. (From the Apache Java Project website).

4.2.2. Download


Apache JServ can be obtained from
http://java.apache.org/jserv/index.html.


>From the Apache JServ Project home page, follow the Download Apache
JServ link.

Currently RPM distributions are available for RedHat Linux. For other
Linux distributions you will have to build from source. The following
example describes how install the RPM for RedHat 6x.

As of the time of this writing, the current version is 1.1b3.

4.2.3. Installation


I suggest installing files in the /usr/local directory. After
downloading the files, run:

mkdir /usr/local/apachejserv
mv ApacheJServ*.rpm /usr/local/apachejserv



For RedHat and RedHat-derived distributions, use the RedHat Package
Manager (RPM) to install the rpm file such as:

rpm -i ApacheJServ-1.1-b2_RH6x.i386.rpm



The above example shows version 1.1-b2 for the RedHat 6x on the Intel
architecture.


4.2.4. Setting up Your Environment


You will need to stop, set your Java environment variables, and
restart Apache to register Apache JServ.

To stop Apache, type:

cd /etc/rc.d/init.d
./httpd stop



Note: The httpd script used at boot time may be in a different
location on other Linux distributions

To set you Java environment, see the How to Install the JDK section of
this document, specifically for the JDK you intend to use. You need
to set several properties in the jserv.properties file installed in
/etc/httpd/conf/jserv. Specifically, look for:

· wrapper.bin - to reference the JDK you installed

· wrapper.classpath - to minimally include
/usr/lib/apache/ApacheJServ.jar and
/home/httpd/classes/servlet-2.0.jar

· bindaddress=localhost

· port=8007

To restart Apache, type:

cd /etc/rc.d/init.d
./httpd start



4.2.5. Confirming Your Installation


To confirm that the Apache JServ is installed and running on your
computer, open your web browser, and enter the URL:
"http://127.0.0.1/servlet/IsItWorking" substituting the correct IP
address if you are browsing from another machine.


You should see a web page to the effect of "Yes, It's Working!".
Contratulations, you have installed, set up an environment for, and
tested the Apache JServ for Linux.

For more assistance, I suggest you look into the Apache JServ website
at

http://java.apache.org/jserv/index.html.


Now, to compile and run your own servlet. Enter the following Java
servlet program.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {
public void service (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}



4.2.6. More Information


For more information, I suggest you look into the Java Apache Project
website at http://java.apache.org/.


4.3. BEA WebLogic


See ``BEA WegLogic ''below.

4.4. Enhydra


To be written.

See http://www.enhydra.orgfor more information.

4.5. IBM WebSphere


To be written.

See http://www-4.ibm.com/software/webservers/appserv/linux.htmlfor
more information.

4.6. Locomotive


To be written.

See ://www.locomotive.org/for more information.


4.7. Jetty

The Jetty HTTP server is a combined server and servlet container.
Installation of the HTTP server (see above) provides servlet support.
More information can be obtained via the demo server and tutorial
installed with the HTTP server.



5. How to Setup Java Server Pages (JSP) Support


To be written.

5.1. Apache Jakarta


To be written.

See ://jakarta.apache.com/for more information.

5.2. Caucho Resin


To be written.

See ://www.caucho.com/for more information.


5.3. Jetty

The Jetty HTTP server comes with the Jasper JSP engine. Installation
of the HTTP server (see 3.4 above) provides JSP support. More
information can be obtained via the demo server and tutorial installed
with the HTTP server.



6. How to Setup JDBC Support


There are several databases that run on Linux that also support a JDBC
interface. These include:

· ``IBM DB2 ''

· ``MiniSQL ''

· ``MySQL ''

· ``Oracle ''

· ``PostgreSQL ''

· ``Sybase ''

If you are going to try just one DBMS, I suggest you initially try
PostgreSQL, principally because it comes with most major Linux
distributions and may already be installed on your system.

6.1. IBM DB2


To be written.

See http://www-4.ibm.com/software/data/db2/linux/for more information.

6.2. MiniSQL


To be written.

See http://www.hughes.com.au/for more information.

6.3. MySQL


To be written.

See http://www.mysql.org/for more information.

6.4. Oracle


To be written.

See http://platforms.oracle.com/linux/

6.5. PostgreSQL


6.5.1. Background


PostgreSQL is a sophisticated Object-Relational DBMS, supporting
almost all SQL constructs, including subselects, transactions, and
user-defined types and functions. It is the most advanced open-source
database available anywhere. Commercial Support is also available
from PostgreSQL, Inc. The current version is 6.5.3 and is available
at any of the many mirror sites or on CD. (From the PostgreSQL
website.)

PostgreSQL may have already been shipped with your Linux distribution
because of its open source license.
6.5.2. Download and Installation


Rather than downloading from PostgreSQL, I suggest you initially try
the PostgreSQL that most likely came with your Linux distribution.

Alternatively, PostgreSQL can be obtained from
http://www.postgresql.org.


To confirm that PostgreSQL is installed on your computer, type:

rpm -qa | grep postgresql



or

which postmaster
which psql



You need the postgresql, postgresql-server, and postgresql-java
packages installed to use Java with PostgreSQL.

Make sure PostgreSQL is running. Type:

ps -f -u postgres



You should see postmaster, the PostgreSQL daemon, running.

If postmaster is not running, there will probably be a Sys V Init
script that you can use to start it. In many distributions it is
located in /etc/rc.d/init.d. To start PostgreSQL, type:

cd /etc/rc.d/init.d
./postgresql start



You can use the above "ps" command to confirm that PostgreSQL is
running.

Note: To use JDBC, PostgreSQL needs to have been started with the '-i'
parameter indicating support for TCP/IP connections rather than solely
UNIX domain sockets. Confirm that postmaster> was started with the
'-i' paramter.


Create a test database by typing:

su - postgres
createdb javatest



You should see no error messages.

Create a test table with one test row. First, log in to the
interactive PostgreSQL tool and connect to the javatest database you
just created by typing (as the postgres user):


psql javatest



You should see confirmation that you are connected to the database:
javatest.


Then, create the test table by typing (within psql):

create table test (col1 varchar(255));



You should see the "CREATE" confirmation message.

Next, insert one row by typing (within psql):

insert into test (col1) values ('Hello, from PostgreSQL!');



You should see the "INSERT" confirmation message.

Finally, confirm that the row is there by typing (within psql):

select col1 from test;



You should see the row you just created.

You can exit psql by typing "\ q".

For more assistance on working with PostgreSQL, I suggest you look
into the Database-SQL-RDBMS HOW-TO document for Linux (PostgreSQL
Object Relational Database System) at
http://metalab.unc.edu/mdw/HOWTO/PostgreSQL-HOWTO.html.


You will need to add the appropriate JAR to your CLASSPATH. The
PostgreSQL JARs come in the postgresql-jdbc package.

export CLASSPATH=$CLASSPATH:/usr/lib/pgsql/jdbc6.5-1.2.jar



You may need to substitute the path depending you where PostgreSQL is
installed on your system.

6.5.3. Confirming Your Installation


You are now ready to compile and run a simple JDBC application that
uses PostgreSQL. Create the following program.



import java.sql.*;

class PostgreSQLTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)
Class.forName("postgresql.Driver").newInstance();
DriverManager.registerDriver(driver);

String url = "jdbc:postgresql:javatest";
Connection con = DriverManager.getConnection(url, "postgres", "");
Statement stm = con.createStatement();

stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery("select col1 from test");

rs.next();

System.out.println(rs.getString(1));

} catch (SQLException e) {

System.out.println("Exception!");
System.out.println(e.toString());
}
}



Compile the program with the Java compiler.

javac PostgreSQLTest.java



If the compiler produces errors, double check the syntax and confirm
your PATH and CLASSPATH.

Run the program with the JVM.

java PostgreSQLTest



If the JVM produces errors, confirm your PATH and CLASSPATH.

You should see the following output:

Hello, from PostgreSQL!



Congratulations, you have installed, set up an environment for, and
tested a JDBC interface to PostgreSQL.

6.5.4. More Information


For more information, I suggest you look into the PostgreSQL website
at

http://www.postgresql.org/.



6.6. Sybase


6.6.1. Background


Sybase Adaptive Server Enterprise is a commericial RDBMS that is
available for the Linux operating system. While Sybase has recently
released version 12.0, version 11.9.2 is available for Linux.

According to the Sybase website, "By porting ASE to Linux, Sybase
provides the Linux development community with the first highly
scalable, high-performance database engine available for the platform.
The package includes the standard features of Adaptive Server
Enterprise and all related connectivity components. Adaptive Server
Enterprise 11.9.2 is offered FREE for development."


6.6.2. Download


The Sybase ASE can be obtained from
http://www.sybase.com/products/databaseservers/linux/linux1192_reg.html.


In order to download, you will have to register with the Sybase
website and agree to the license online.

The Sybase JDBC driver can be obtained from
http://www.sybase.com/products/internet/jconnect/.


Select download jConnect 4.2/5.2.

If you have access to a Sybase server on the network, you only need to
download and install the JDBC driver.

6.6.3. Installation


Installation of Sybase is beyond the scope of this HOWTO. This HOWTO
will assume that Sybase has been correctly installed and configured
and that you can get to Sybase using isql.

Log into isql as sa and create a test user and test database by
typing:


create database javatest
go
sp_addlogin javatest, javatest, javatest
go
use javatest
go
sp_dbowner javatest
go



You should see no error messages.

Create a test table with one test row. First, log in to isql as the
javatest user and type:



create table test (col1 varchar(255))
go



You should see no error messages.

Next, insert one row by typing:

insert into test (col1) values ('Hello, from Sybase!')
go



You should see no error messages.

Finally, confirm that the row is there by typing:

select col1 from test
go



You should see the row you just created.

You can exit isql by typing "exit".

For more assistance on working with Sybase, review the documentation
that can be downloaded with Sybase.

You will need to add the appropriate JAR to your CLASSPATH.

export CLASSPATH=$CLASSPATH:/usr/local/sybase/jConnect-5_2/classes/jconn2.jar



You may need to substitute the path depending you where jConnect is
installed on your system.

6.6.4. Confirming Your Installation


You are now ready to compile and run a simple JDBC application that
uses Sybase. Create the following program.



import java.sql.*;

class SybaseTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)
Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance();
DriverManager.registerDriver(driver);

String host = "127.0.0.1";
String port = "4100";

String url = "jdbc:sybase:Tds:" + host + ":" + port;
Connection con = DriverManager.getConnection(url, "javatest", "javatest");
Statement stm = con.createStatement();

stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery("select col1 from test");

rs.next();

System.out.println(rs.getString(1));

} catch (SQLException e) {

System.out.println("Exception!");
System.out.println(e.toString());
}
}



You will need to substitute the host and port number of you Sybase
server as appropriate. See $SYBASE/interfaces and the $DSQUERY entry
for what values to use for the host and port number.

Compile the program with the Java compiler.

javac SybaseTest.java



If the compiler produces errors, double check the syntax and confirm
your PATH and CLASSPATH.

Run the program with the JVM.

java SybaseTest



If the JVM produces errors, confirm your PATH and CLASSPATH.

You should see the following output:

Hello, from Sybase!



Congratulations, you have installed, set up an environment for, and
tested a JDBC interface to Sybase.

6.6.5. More Information



For more information, I suggest you look into the Sybase jConnect
website at http://www.sybase.com/products/internet/jconnect/.


7. How to Setup Enterprise Java Bean (EJB) Support


To be written.

7.1. BEA WebLogic


To be written.

See http://www.beasys.com/linux/for more information.


7.2. EJBoss


7.2.1. Background

EJBoss has been renamed JBoss and is well advanced with stable J2EE
compliant releases at http://www.jboss.org/.

This section was written when it was still EJBoss 0.95 and needs to be
updated.


7.2.2. Download


JBoss can be downloaded from the JBoss website at
http://www.jboss.org/.


7.2.3. Installation


I suggest installing files in the /usr/local directory. After
downloading, run:

mkdir /usr/local/ejboss
mv ejboss* /usr/local/ejboss



Unjar the file:

jar xvf ejboss095_jdk122.jar



You should see various files and directories created under
/usr/local/ejboss.

The above example shows EJBoss 0.95 for JDK 1.2.2. Substitute the file
names as appropriate.

7.2.4. Setting up Your Environment


The environment variables to set up are:

· CLASSPATH

The CLASSPATH environment variable references all JARs and directories
that you will need to compile and run Java programs.

Include the EJBoss JAR and the beans/generated directory in your
CLASSPATH.

export CLASSPATH=/usr/local/ejboss/lib/ejboss095_jdk122.jar:/usr/local/ejboss/beans/generated:$CLASSPATH



7.2.5. Confiming Your Installation


You are now ready to compile and run a simple EJB application. Create
the following three source files for the server.

First, the business interface.

// EJBTest.java

import javax.ejb.*;
import java.rmi.RemoteException;

public
interface EJBTest extends EJBObject {
public String greet() throws
RemoteException;

}



Second, the home interface.

// EJBTestHome.java

import javax.ejb.*;
import java.rmi.RemoteException;

public
interface EJBTestHome extends EJBHome {

public EJBTest create() throws

CreateException, RemoteException;
}



Third, the bean implementation class.



// EJBTestBean.java

import javax.ejb.*;
import java.rmi.RemoteException;

public
interface EJBTestBean implements SessionBean {

private SessionContext
mContext = null;

public void ejbPassivate() {
System.out.println("EJBTestBean
passivated.");
}

public void ejbActivate() {
System.out.println("EJBTestBean
activated.");
}

public void ejbCreate() {
System.out.println("EJBTestBean
created.");
}

public void ejbRemove() {
System.out.println("EJBTestBean
removed.");
}

public void setSessionContext() {
System.out.println("EJBTestBean
context set.");
mContext = context;
}

public String greet()
{
return "Hello, I'm an EJB!";
}

}



Compile the server source files with the Java compiler:

javac EJBTest*.java



If the compiler produces errors, double check the syntax and confirm
your PATH and CLASSPATH.

Now that you have successfully written and compiled the server source
files, you need to deploy your bean to EJBoss. Deploying a bean to
EJBoss requires several steps that must be performed exactly.

First, create the file ejb-jar.xml.





































Nextgen bean



nextgen.EJBTest



EJBTestHome



EJBTest



EJBTestBean



Stateful



Bean













java.lang.String



























Container























The above file, which must be named ejb-jar.xml identifies the
interface and class names of files that you just created as well as a
name for the object.

Second, relative to the directory of the three class files you just
created, create a META-INF directory.

mkdir META-INF
mv ejb-jar.xml META-INF

Third, package all four files into a jar.

jar cvf EJBTest.jar EJBTest*.class META-INF/ejb-jar.xml



You should see that it added the manifest as well as the three class
files and the XML deployment descriptor file.

Fourth, put the JAR you just created in the EJBoss beans directory.

mv EJBTest.jar /usr/local/ejboss/beans



Fifth, move the class files you created to the EJBoss beans/generated
directory.

mv EJBTest*.class /usr/local/ejboss/beans/generated



(This fifth step is redudant due to a bug in EJBoss 0.95. )

You are now ready to start the EJBoss server.

cd /usr/local/ejboss

sh server.sh

You should see the proxy files compile automatically and confirmation
that your EJB is deployed.

You are now ready to write, compile and test the simple client
applicaiton.

7.3. Bullsoft JOnAS EJB

Java Interview Questions

Describe what happens when an object is created in Java?

Several things happen in a particular order to ensure the object is constructed properly:
1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implementation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its uppercase. This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

In Java, you can create a String object as below : String str = "abc"; & String str = new String("abc"); Why cant a button object be created as : Button bt = "abc"? Why is it compulsory to create a button object as: Button bt = new Button("abc"); Why this is not compulsory in String’s case?

Button bt1= "abc"; It is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a Button object. That simple. The only object in Java that can be assigned a literal String is java.lang.String. Important to not that you are NOT calling a java.lang.String constuctor when you type String s = "abc"; For example String x = "abc"; String y = "abc"; refer to the same object. While String x1 = new String("abc");
String x2 = new String("abc"); refer to two different objects.

What is the advantage of OOP?

You will get varying answers to this question depending on whom you ask. Major advantages of OOP are:
1. Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear;
2. Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system;
3. Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods;
4. Extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones;
5. Maintainability: objects can be maintained separately, making locating and fixing problems easier;
6. Re-usability: objects can be reused in different programs

What are the main differences between Java and C++?

Everything is an object in Java( Single root hierarchy as everything gets derived from java.lang.Object). Java does not have all the complicated aspects of C++ ( For ex: Pointers, templates, unions, operator overloading, structures etc..) The Java language promoters initially said "No pointers!", but when many programmers questioned how you can work without pointers, the promoters began saying "Restricted pointers." You can make up your mind whether it’s really a pointer or not. In any event, there’s no pointer arithmetic. There are no destructors in Java. (automatic garbage collection), Java does not support conditional compile (#ifdef/#ifndef type). Thread support is built into java but not in C++. Java does not support default arguments. There’s no scope resolution operator :: in Java. Java uses the dot for everything, but can get away with it since you can define elements only within a class. Even the method definitions must always occur within a class, so there is no need for scope resolution there either. There’s no "goto " statement in Java. Java doesn’t provide multiple inheritance (MI), at least not in the same sense that C++ does. Exception handling in Java is different because there are no destructors. Java has method overloading, but no operator overloading. The String class does use the + and += operators to concatenate strings and String expressions use automatic type conversion, but that’s a special built-in case. Java is interpreted for the most part and hence platform independent

What are interfaces?

Interfaces provide more sophisticated ways to organize and control the objects in your system.
The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but an interface says: “This is what all classes that implement this particular interface will look like.” Thus, any code that uses a particular interface knows what methods might be called for that interface, and that’s all. So the interface is used to establish a “protocol” between classes. (Some object-oriented programming languages have a keyword called protocol to do the same thing.) Typical example from "Thinking in Java":

import java.util.*;
interface Instrument {
int i = 5; // static & final
// Cannot have method definitions:
void play(); // Automatically public
String what();
void adjust();
}
class Wind implements Instrument {
public void play() {
System.out.println("Wind.play()");
public String what() { return "Wind"; }
public void adjust() {}
}

How can you achieve Multiple Inheritance in Java?

Java’s interface mechanism can be used to implement multiple inheritance, with one important difference from c++ way of doing MI: the inherited interfaces must be abstract. This obviates the need to choose between different implementations, as with interfaces there are no implementations.

interface CanFight {
void fight();
interface CanSwim {
void swim();
interface CanFly {
void fly();
class ActionCharacter {
public void fight() {}
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
}

You can even achieve a form of multiple inheritance where you can use the *functionality* of classes rather than just the interface:

interface A {
void methodA();
}
class AImpl implements A {
void methodA() { //do stuff }
}
interface B {
void methodB();
}
class BImpl implements B {
void methodB() { //do stuff }
}
class Multiple implements A, B {
private A a = new A();
private B b = new B();
void methodA() { a.methodA(); }
void methodB() { b.methodB(); }
}
This completely solves the traditional problems of multiple inheritance in C++ where name clashes occur between multiple base classes. The coder of the derived class will have to explicitly resolve any clashes. Don’t you hate people who point out minor typos? Everything in the previous example is correct, except you need to instantiate an AImpl and BImpl. So class Multiple would look like this:
class Multiple implements A, B {
private A a = new AImpl();
private B b = new BImpl();
void methodA() { a.methodA(); }
void methodB() { b.methodB(); }
}

What is the difference between StringBuffer and String class?

A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. The String class represents character strings. All string literals in Java programs, such as "abc" are constant and implemented as instances of this class; their values cannot be changed after they are created. Strings in Java are known to be immutable. What it means is that every time you need to make a change to a String variable, behind the scene, a "new" String is actually being created by the JVM. For an example: if you change your String variable 2 times, then you end up with 3 Strings: one current and 2 that are ready for garbage collection. The garbage collection cycle is quite unpredictable and these additional unwanted Strings will take up memory until that cycle occurs. For better performance, use StringBuffers for string-type data that will be reused or changed frequently. There is more overhead per class than using String, but you will end up with less overall classes and consequently consume less memory. Describe, in general, how java’s garbage collector works? The Java runtime environment deletes objects when it determines that they are no longer being used. This process is known as garbage collection. The Java runtime environment supports a garbage collector that periodically frees the memory used by objects that are no longer needed. The Java garbage collector is a mark-sweep garbage collector that scans Java’s dynamic memory areas for objects, marking those that are referenced. After all possible paths to objects are investigated, those objects that are not marked (i.e. are not referenced) are known to be garbage and are collected. (A more complete description of our garbage collection algorithm might be "A compacting, mark-sweep collector with some conservative scanning".) The garbage collector runs synchronously when the system runs out of memory, or in response to a request from a Java program. Your Java program can ask the garbage collector to run at any time by calling System.gc(). The garbage collector requires about 20 milliseconds to complete its task so, your program should only run the garbage collector when there will be no performance impact and the program anticipates an idle period long enough for the garbage collector to finish its job. Note: Asking the garbage collection to run does not guarantee that your objects will be garbage collected. The Java garbage collector runs asynchronously when the system is idle on systems that allow the Java runtime to note when a thread has begun and to interrupt another thread (such as Windows 95). As soon as another thread becomes active, the garbage collector is asked to get to a consistent state and then terminate.

What’s the difference between == and equals method?

equals checks for the content of the string objects while == checks for the fact that the two String objects point to same memory location ie they are same references.

What are abstract classes, abstract methods?

Simply speaking a class or a method qualified with "abstract" keyword is an abstract class or abstract method. You create an abstract class when you want to manipulate a set of classes through a common interface. All derived-class methods that match the signature of the base-class declaration will be called using the dynamic binding mechanism. If you have an abstract class, objects of that class almost always have no meaning. That is, abstract class is meant to express only the interface and sometimes some default method implementations, and not a particular implementation, so creating an abstract class object makes no sense and are not allowed ( compile will give you an error message if you try to create one). An abstract method is an incomplete method. It has only a declaration and no method body. Here is the syntax for an abstract method declaration: abstract void f(); If a class contains one or more abstract methods, the class must be qualified an abstract. (Otherwise, the compiler gives you an error message.). It’s possible to create a class as abstract without including any abstract methods. This is useful when you’ve got a class in which it doesn’t make sense to have any abstract methods, and yet you want to prevent any instances of that class. Abstract classes and methods are created because they make the abstractness of a class explicit, and tell both the user and the compiler how it was intended to be used.
For example:
abstract class Instrument {
int i; // storage allocated for each
public abstract void play();
public String what() {
return "Instrument";
public abstract void adjust();
}
class Wind extends Instrument {
public void play() {
System.out.println("Wind.play()");
}
public String what() { return "Wind"; }
public void adjust() {}
Abstract classes are classes for which there can be no instances at run time. i.e. the implementation of the abstract classes are not complete. Abstract methods are methods which have no defintion. i.e. abstract methods have to be implemented in one of the sub classes or else that class will also become Abstract.

What is the difference between an Applet and an Application?

A Java application is made up of a main() method declared as public static void that accepts a string array argument, along with any other classes that main() calls. It lives in the environment that the host OS provides. A Java applet is made up of at least one public class that has to be subclassed from java.awt.Applet. The applet is confined to living in the user’s Web browser, and the browser’s security rules, (or Sun’s appletviewer, which has fewer restrictions). The differences between an applet and an application are as follows:
1. Applets can be embedded in HTML pages and downloaded over the Internet whereas Applications have no special support in HTML for embedding or downloading.
2. Applets can only be executed inside a java compatible container, such as a browser or appletviewer whereas Applications are executed at command line by java.exe or jview.exe.
3. Applets execute under strict security limitations that disallow certain operations(sandbox model security) whereas Applications have no inherent security restrictions.
4. Applets don’t have the main() method as in applications. Instead they operate on an entirely different mechanism where they are initialized by init(),started by start(),stopped by stop() or destroyed by destroy().

Java says "write once, run anywhere". What are some ways this isn’t quite true?

As long as all implementaions of java are certified by sun as 100% pure java this promise of "Write once, Run everywhere" will hold true. But as soon as various java core implemenations start digressing from each other, this won’t be true anymore. A recent example of a questionable business tactic is the surreptitious behavior and interface modification of some of Java’s core classes in their own implementation of Java. Programmers who do not recognize these undocumented changes can build their applications expecting them to run anywhere that Java can be found, only to discover that their code works only on Microsoft’s own Virtual Machine, which is only available on Microsoft’s own operating systems.

What is the difference between a Vector and an Array. Discuss the advantages and disadvantages of both?

Vector can contain objects of different types whereas array can contain objects only of a single type.
- Vector can expand at run-time, while array length is fixed.
- Vector methods are synchronized while Array methods are not

What are java beans?

JavaBeans is a portable, platform-independent component model written in the Java programming language, developed in collaboration with industry leaders. It enables developers to write reusable components once and run them anywhere — benefiting from the platform-independent power of Java technology. JavaBeans acts as a Bridge between proprietary component models and provides a seamless and powerful means for developers to build components that run in ActiveX container applications. JavaBeans are usual Java classes which adhere to certain coding conventions:
1. Implements java.io.Serializable interface
2. Provides no argument constructor
3. Provides getter and setter methods for accessing it’s properties

What is RMI?

RMI stands for Remote Method Invocation. Traditional approaches to executing code on other machines across a network have been confusing as well as tedious and error-prone to implement. The nicest way to think about this problem is that some object happens to live on another machine, and that you can send a message to the remote object and get a result as if the object lived on your local machine. This simplification is exactly what Java Remote Method Invocation (RMI) allows you to do. Above excerpt is from "Thinking in java". For more information refer to any book on Java.

What does the keyword "synchronize" mean in java. When do you use it? What are the disadvantages of synchronization?

Synchronize is used when you want to make your methods thread safe. The disadvantage of synchronize is it will end up in slowing down the program. Also if not handled properly it will end up in dead lock.

What gives java it’s "write once and run anywhere" nature?

Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platorm specific and hence can be fed to any platform. After being fed to the JVM, which is specific to a particular operating system, the code platform specific machine code is generated thus making java platform independent.

What are native methods? How do you use them?

Native methods are methods written in other languages like C, C++, or even assembly language. You can call native methods from Java using JNI. Native methods are used when the implementation of a particular method is present in language other than Java say C, C++. To use the native methods in java we use the keyword native
public native method_a(). This native keyword is signal to the java compiler that the implementation of this method is in a language other than java. Native methods are used when we realize that it would take up a lot of rework to write that piece of already existing code in other language to Java.

What is JDBC? Describe the steps needed to execute a SQL query using JDBC.

We can connect to databases from java using JDBC. It stands for Java DataBase Connectivity.
Here are the steps:
1. Register the jdbc driver with the driver manager
2. Establish jdbc connection
3. Execute an sql statement
4. Process the results
5. Close the connection
Before doing these do import java.sql.*
JDBC is java based API for accessing data from the relational databases. JDBC provides a set of classes and interfaces for doing various database operations. The steps are:
Register/load the jdbc driver with the driver manager.
Establish the connection thru DriverManager.getConnection();
Fire a SQL thru conn.executeStatement();
Fetch the results in a result set
Process the results
Close statement/result set and connection object.

How many different types of JDBC drivers are present? Discuss them.

There are four JDBC driver types.
Type 1: JDBC-ODBC Bridge plus ODBC Driver:
The first type of JDBC driver is the JDBC-ODBC Bridge. It is a driver that provides JDBC access to databases through ODBC drivers. The ODBC driver must be configured on the client for the bridge to work. This driver type is commonly used for prototyping or when there is no JDBC driver available for a particular DBMS.
Type 2: Native-API partly-Java Driver:
The Native to API driver converts JDBC commands to DBMS-specific native calls. This is much like the restriction of Type 1 drivers. The client must have some binary code loaded on its machine. These drivers do have an advantage over Type 1 drivers because they interface directly with the database.
Type 3: JDBC-Net Pure Java Driver:
The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network protocol that is sent to a middleware server. This server then translates this DBMS-independent protocol into a DBMS-specific protocol, which is sent
to a particular database. The results are then routed back through the middleware server and sent back to the client. This type of solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client.
Type 4: Native-Protocol Pure Java Driver
These are pure Java drivers that communicate directly with the vendor’s database. They do this by converting JDBC commands directly into the database engine’s native protocol. This driver has no additional translation or middleware layer, which improves performance tremendously.
What does the "static" keyword mean in front of a variable? A method? A class? Curly braces {}?
static variable
- means a class level variable
static method:
-does not have "this". It is not allowed to access the not static members of the class.
can be invoked enev before a single instance of a class is created.
eg: main
static class:
no such thing.
static free floating block:
is executed at the time the class is loaded. There can be multiple such blocks. This may be useful to load native libraries when using native methods.
eg:
native void doThis(){
static{
System.loadLibrary("myLibrary.lib");
}

Access specifiers: "public", "protected", "private", nothing?

In the case of Public, Private and Protected, that is used to describe which programs can access that class or method: Public – any other class from any package can instantiate and execute the classes and methods. Protected – only subclasses and classes inside of the package can access the classes and methods. Private – the original class is the only class allowed to executed the methods.

What does the "final" keyword mean in front of a variable? A method? A class?

FINAL for a variable : value is constant
FINAL for a method : cannot be overridden
FINAL for a class : cannot be derived
A final variable cannot be reassigned,
but it is not constant. For instance,
final StringBuffer x = new StringBuffer()
x.append("hello");
is valid. X cannot have a new value in it,but nothing stops operations on the object
that it refers, including destructive operations. Also, a final method cannot be overridden
or hidden by new access specifications.This means that the compiler can choose
to in-line the invocation of such a method.(I don’t know if any compiler actually does
this, but it’s true in theory.) The best example of a final class is
String, which defines a class that cannot be derived.

Does Java have "goto"?

No.

What synchronization constructs does Java provide? How do they work?

The two common features that are used are:
1. Synchronized keyword - Used to synchronize a method or a block of code. When you synchronize a method, you are in effect synchronizing the code within the method using the monitor of the current object for the lock.
The following have the same effect.
synchronized void foo() {
}
and
void foo() {
synchronized(this) {
}
If you synchronize a static method, then you are synchronizing across all objects of the same class, i.e. the monitor you are using for the lock is one per class, not one per object.
2. wait/notify. wait() needs to be called from within a synchronized block. It will first release the lock acquired from the synchronization and then wait for a signal. In Posix C, this part is equivalent to the pthread_cond_wait method, which waits for an OS signal to continue. When somebody calls notify() on the object, this will signal the code which has been waiting, and the code will continue from that point. If there are several sections of code that are in the wait state, you can call notifyAll() which will notify all threads that are waiting on the monitor for the current object. Remember that both wait() and notify() have to be called from blocks of code that are synchronized on the monitor for the current object.

Does Java have multiple inheritance?

Java does not support multiple inheritence directly but it does thru the concept of interfaces.
We can make a class implement a number of interfaces if we want to achieve multiple inheritence type of functionality of C++.

How does exception handling work in Java?

1.It separates the working/functional code from the error-handling code by way of try-catch clauses.
2.It allows a clean path for error propagation. If the called method encounters a situation it can’t manage, it can throw an exception and let the calling method deal with it.
3.By enlisting the compiler to ensure that "exceptional" situations are anticipated and accounted for, it enforces powerful coding.
4.Exceptions are of two types: Compiler-enforced exceptions, or checked exceptions. Runtime exceptions, or unchecked exceptions. Compiler-enforced (checked) exceptions are instances of the Exception class or one of its subclasses — excluding the RuntimeException branch. The compiler expects all checked exceptions to be appropriately handled. Checked exceptions must be declared in the throws clause of the method throwing them — assuming, of course, they’re not being caught within that same method. The calling method must take care of these exceptions by either catching or declaring them in its throws clause. Thus, making an exception checked forces the us to pay heed to the possibility of it being thrown. An example of a checked exception is java.io.IOException. As the name suggests, it throws whenever an input/output operation is abnormally terminated.

Does Java have destructors?

Garbage collector does the job working in the background
Java does not have destructors; but it has finalizers that does a similar job.
the syntax is
public void finalize(){
}
if an object has a finalizer, the method is invoked before the system garbage collects the object

What does the "abstract" keyword mean in front of a method? A class?

Abstract keyword declares either a method or a class.
If a method has a abstract keyword in front of it,it is called abstract method.Abstract method hs no body.It has only arguments and return type.Abstract methods act as placeholder methods that are implemented in the subclasses.
Abstract classes can’t be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract

Are Java constructors inherited ? If not, why not?

You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it’s superclasses. One of the main reasons is because you probably don’t want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.