A basic Application to get started – part 1

What is our code doing?

Now you may be thinking that we have just completed a basic application in the previous section and whilst that is true, that was all about proving our environment works.  Now we are going to assume our tools work and take a look at building our application.

As any experienced developer will tell you, when things go wrong with your application, it’s too late to wish you had some information to tell you what is going on; develop a strategy for capturing what your application is doing behind the scenes and implement it throughout your code.

For this application we are going to use a log file to write out what’s going on, more specifically we are going to use Log4J that we downloaded earlier.

I spoke earlier about a strategy; the strategy I will use here is to make use of two levels of logging, Info and Debug.  The debugging option will reside in a configuration file so that it may easily be altered to suit.  Info will provide flow information, key data values and actions and is intended to be used when the application is deployed.  Debug provides more detailed data values and information about which branches of code are executed.  This is intended to help developers trace issues during development and post deployment.

So to implement our strategy we are going to take our ‘Hello World’ app and apply logging.  As the application is developed and enhanced the logging will expand and extend with the code changes.

First off we need to create a new project.  We will create a new project each time we add a new section to our application.  Follow the steps to create a project used in ‘Testing our environment – part 1’ but this time I will name the project ‘Build An App2’.

Next add the Log4J JAR file to the project so the tools are available to the program.  Go to the project Properties and select ‘Java Build Path’ from the left hand navigator and then select the ‘Libraries’ tab which will display a list of all libraries available to the project.


Press the button ‘Add External JARS…’, navigate to the location where the Log4J JAR file was placed, select and press the OK button to add the JAR to the project.  Close the project properties by clicking the OK button.

Create the main application class as we did in ‘Testing our environment – part 2’ so I called my class ‘Application2b’ and placed it in the ‘softwarepulse.app’ package.  Ensure you select ‘public static void main(String[] args) for this class.

So what we should have is this:


Now open up our application class from the previous stage.  Copy the code from under the main method and paste into our new class.  We will reuse this code here.

Note: Each section of Building A Java Desktop Application will build on the previous code so, for those of you who are familiar with version control software you might like to use this to build on the previous version.  If you are not familiar with version control software and do not wish to take on that challenge at this time do not worry.  I originally built this by creating a new project for each section and copying the code into the new project.

So our main Application code should look like this:


Now let’s add some logging to the application.

First we create a variable of type Logger and assign it a logger with the name of our class.

static Logger log = Logger.getLogger(BuildAnApp1.class);

Next we need to create a properties file for our Logger.  Directly under the project create a file and call it ‘log4j.properties’.  Then populate the file with the following text:

log4j.rootLogger=DEBUG, A1
###### Debugging levels

########## Appender A1
# Append to the end of the file or overwrites the file at start.
log4j.appender.A1.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n

########## Appender A2
# Roll the log file at a certain time
# Append to the end of the file or overwrites the file at start.
log4j.appender.A2.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n

Within the file there are two formats for logging information, A1 and A2.  The A1 setup I use when developing and testing code.  Each time the code is run it over writes the pervious information. The A2 setting is designed for use once deployed where a new file is created each day and entries for the current day are appended to the file, keeping a record of activity during the day.

From the first line in the file it can be seen that the logging level is set to DEBUG, once deployed you can move it to WARN and then if a user encounters an issue it can be changed to INFO to gather more detailed information.

For both the logging formats there is a directory called ‘log’ specified.  The log files will be created in the log directory, a subdirectory from the root directory of the application.

For more explanation about the settings in the properties file reference the log4j documentation.  These are fairly basic settings and the tool supports many more options.

So with the Log4j properties file created we now need to reference this file from our code. In the main method of our application the first thing to do is load the properties so our logging uses them.


Then write some text out to the log file so we know our application is using the logging.

log.info("***** Build An Application Started *****");

To quickly test this is working save the code and run the application from Eclipse.  The console will display “The start of our application.” which is unchanged from our earlier example.  What is different is that in the Eclipse workspace under the folder for the project there will now be a new folder called ‘log’ and in that folder will be our log file.


Opening the log file will show the following text:


The our code so far looks like this:


A basic Application to get started – part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s