Training

Developing Java Applications in WSAD v5.1.2 Using JDK 1.4 Tutorial

Popular Courses

Browse Our Free Resources

  • whitepapers
  • whitepapers
  • webinars
  • blogs

Our Locations

Training Centres

Vancouver, BC
Calgary, AB
Edmonton, AB
Toronto, ON
Ottawa, ON
Montreal, QC
Hunt Valley
Columbia

locations map

Calgary

550 6th Av SW
Suite 475
Calgary, AB
T2P 0S2

Toronto

821A Bloor Street West
Toronto, ON
M6G 1M1

Vancouver

409 Granville St
Suite 902
Vancouver, BC
V6C 1T2

U.S. Office

436 York Road
Suite 1
Jenkintown, PA
19046

Other Locations

Dallas, TX
Miami, FL

Web Age Solutions Inc.

Introduction

By default, WSAD v5.1.2 uses JRE 1.3 to compile and execute a Java project. WSAD (Eclipse) has the capability to associate a project with any JRE installed in the hard drive. Keep in mind, the associate JRE is used for compilation in the sense that the system JAR file (such as rt.jar) are added to the compiler's class path. The compiler itself is vendor neutral and supplied by Eclipse. This compiler is capable of generating JDK 1.4 specific byte code or backward compatible bytecode. The compile can not be easily switched and there is no real need to switch the compiler.

Also, Eclipse uses the JVM from the JRE associated with a project to execute or debug the project.

When you install WSAD v5.1.2, you get the WebSphere v5.1 run time. WebSphere v5.1 installs IBM's JDK 1.4. In this tutorial, we will setup a Java Project to use IBM's JDK 1.4.

Create a Project

Launch WSAD v5.1.2. From the menubar select File->New->Project. Select Java and Java Project. Click on Next. As the project's name, enter JDK14Sampler. Click on Finish. System will prompt you to switch to the Java perspective.

Click on Yes.

Associate the Project With JRE 1.4

Right click on JDK14Sampler and select Properties. Select the Java Build Path properties. Click on the Libraries tab.

The project is currently using the default JRE that is at a 1.3 level.

Select JRE System Library and click on the Remove button. Click on the Add Library button.

Select JRE System Library and click on Next.

From the JRE list, pick WebSphere v5.1 JRE. Click on Finish. Click on OK.

Next, we will develop a few classes that will exercise certain JDK 1.4 specific features.

Implement XML Serialization

In the Package Explorer view, right click on JDK14Sampler and select New->Package. Enter com.mycom.test as the package name. Click on Finish.

Now, we will create a simple JavaBean. Right click on the newly created package and select New->Class. Enter Customer as the class name. Click on Finish. In the Customer class, enter a fiew member variables as follows.

String name;
int age;

Right click on the editor and select Source->Generate Getter and Setter. Click on Select All. Click on OK.

Make the Customer class serializable by implementing the java.io.Serializable interface.

public class Customer implements java.io.Serializable { 

Save changes (Control+S).

In the same package, create another class called CustomerManager. This class will contain the business logic to maintain a list of customers in a persistent manner.

In the CustomerManager.java file, import the following package names.

import java.util.logging.*; 
import java.beans.*; 
import java.util.*; import java.io.*;

Add a member variable as follows.

ArrayList customerList = new ArrayList();

Add these methods.

public void addCustomer(Customer c) {
   customerList.add(c);
}
public void save(String fileName) throws Exception {
   XMLEncoder enc = new XMLEncoder(
       new BufferedOutputStream(
       new FileOutputStream(fileName)));
   enc.writeObject(customerList);
   enc.close();
}
public void load(String fileName) throws Exception {
   XMLDecoder dec = new XMLDecoder(
       new BufferedInputStream(
       new FileInputStream(fileName)));
   Object o = dec.readObject();
   dec.close();
   
   customerList = (ArrayList) o;
}
public static void main(String[] args) {
   try {
       Customer c = null;
       CustomerManager cm = new CustomerManager();
   
       c = new Customer();
       c.setName("Mike Bouchard");
       c.setAge(25);
       cm.addCustomer(c);
   
       c = new Customer();
       c.setName("Richard Patrick");
       c.setAge(21);
       cm.addCustomer(c);
   
       cm.save("customer.xml");
System.out.println("Data was successfully saved."); } catch (Exception e) { e.printStackTrace(); } }

Save changes (Control+S). Make sure that there are no compilation problems. Note, the use of the load() method is left to the students as an exercise.

Test the Code

Make sure that the CustomerManager.java file is still open in the editor. From the menu bar, select Run->Run As->2 Java Application.

Make sure that you see the success message in the console.

Right click on the JDK14Sampler project and select Refresh. You should see the customer.xml file appear within the project. Double click on the file to open it. Click on the Source tab. Study the structure of the XML file.

Add JDK Logging Code

In the CustomerManager class, add a new member variable as follows.

static Logger logger = Logger.getLogger("MyApplication"); 

In the addCustomer() method, add logging code as shown in bold face below.

public void addCustomer(Customer c) { 
     logger.fine("Adding customer: " + c.getName()); 
     customerList.add(c); 
} 

In the catch block of the main method, replace the following line:

e.printStackTrace(); 

With,

logger.log(Level.SEVERE, "Error encountered", e);

Save changes (Control+S).

Configure Logging

Log configuration is stored in lib/logging.properties within the JRE's home directory. In our case, JRE's home directory is <WSAD Install Directory>\runtimes\base_v51\java\jre\. Using a file explorer, go to that directory. Locate the lib folder. Within the lib folder, locate logging.properties folder. Open the file using notepad.

Notice, how only one handler - a ConsoleHandler - is currently registered.

handlers= java.util.logging.ConsoleHandler 

Change the registered handler to a FileHandler as shown in bold face below.

handlers= java.util.logging.FileHandler

Note, you can register any number of handler classes separated by a comma.

Change the name of the log file.

java.util.logging.FileHandler.pattern = my_application.log

Change the format of the log file from XML to Simple.

java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

Add the following line at the end of the file to enable FINE level logging for the MyApplication logger.

MyApplication.level = FINE

Save and close the file.

Test the Code

Make sure that the CustomerManager.java file is still open in the editor. From the menu bar, select Run->Run As->2 Java Application. Right click on the prject and select Refresh. You should see the my_aplication.log file in the project. Open the file and study the log entry.

Resources

  1. Java Knowledge Base
  2. Java training classes from Web Age Solutions Inc.

Feedback


Email Address: *


Very useful Somewhat useful Not bad Needs many corrections


Comments:
*


( * ) = mandatory field