DEVELOPING JAVA APPLICATIONS IN WSAD V5.1.2 USING JDK 1.4 TUTORIAL

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 Librariestab.

DEVELOPING JAVA>

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>runtimesbase_v51javajre. 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.