Tuesday, November 15, 2011

Eclipse - HelloWorld

Right click on your HelloWorld project and select New Class

In the wizard, enter the package name 'org.confucius' and the class name 'HelloWorld'

A new file HelloWorld.java will be created in the /src/org/confucius folder

Enter the following code - it is the same code which we wrote earlier.
(Notice that this time we have a package org.confucius for the class)

 package org.confucius;  
   
 import org.apache.log4j.BasicConfigurator;  
 import org.apache.log4j.Logger;  
   
   
 public class HelloWorld {  
      public static void main(String[] args) {  
           // Set up a simple configuration that logs on the console.  
           BasicConfigurator.configure();  
           Logger logger = Logger.getLogger(HelloWorld.class);  
           logger.debug("Hello World!");  
      }  
 }  
   


Eclipse will red mark all references to the log4j classes because we do not have log4j.jar in the CLASSPATH

Let us add log4j.jar

Right Click on the HelloWorld project directory, and select New->Folder

Name the folder 'lib'

Copy the log4j.jar to this folder


Right Click on the HelloWorld project directory, and select Properties->Java Build Path->Libraries

Click on 'Add Jars' and select the /lib/log4j.jar

The red marks should all disappear and you will see the HelloWorld.class created in /bin/org/confucius

Right Click on the HelloWorld project directory, and select Run As->Java Application-->HelloWorld

You will see the output in the Eclipse Console.

Note: Open the .classpath and notice the entry for log4j.jar

No comments: