Thursday, 29 November 2012

Logger class in java

For big applications log is very important thing. Using these logs we can detect cause of problem. We can determine the place where problem occurred. For big applications it is necessary to store that log in one of the file. In order to facilitate this java provides Logger class.

This logger class is very easy to use. It allow you to create files in different formats like xml files, simple files etc. This logger class stores lot of information like time stamp, class name, method name and severity of problem.

Here I am giving very simple example one for simple file and another for xml file.

Create simple log file: 
Try to execute following code and you will get log file.

Code:
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Test
{
    FileHandler fileHandler;
    Logger logger;
   
    public Test()
    {
        try
        {
            fileHandler = new FileHandler("test.log");
            logger = Logger.getLogger("log_file");
            logger.addHandler(fileHandler);
            fileHandler.setFormatter(new SimpleFormatter());
        }
        catch(Exception e)
        {
            System.out.print("Exception : " + e);
        }
    }
   
    public static void main(String[] args)
    {
        Test t = new Test();
        t.testingMethod();
        t.anotherMethod();
    }
 
    private void testingMethod()
    {
        try
        {
            logger.warning("See what is happening.");
            logger.info("See again");
            logger.severe("It is problem");
        }
        catch(Exception e)
        {
            System.out.print("Exception : " + e);
        }
    }
   
    private void anotherMethod()
    {
        try
        {
            logger.addHandler(fileHandler);
            fileHandler.setFormatter(new SimpleFormatter());
          
            logger.warning("Test");
            logger.info("Test");
            logger.severe("Test");
        }
        catch(Exception e)
        {
            System.out.print("Exception : " + e);
        }
    }
}



Using this mechanism you can add logs to your applications.

Wednesday, 28 November 2012

Split panel in JAVA

Split panel in java is mainly used to create dynamic dividers or sections on your frame. You can either add vertical or horizontal split. In one split panel you can add only two components. You can add one split panel in another one.

Here I am giving one very simple example of split panel.

Code:
import java.awt.Button;

import javax.swing.JFrame;
import javax.swing.JSplitPane;

class Test
{
    JFrame frm = new JFrame();
    public Test()
    {
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new Button("B2"), new Button("B2"));
      
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(150);

        frm.add(splitPane);

        frm.setSize(500,500);
        frm.setVisible(true);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String args[])
    {
        new Test();
    }
}


Here you can see that I have added two buttons in split panel. Run this program and you will see that you can resize any section as much as you want. Even you can hide one section.

Wednesday, 21 November 2012

Maven

Maven is one of the very useful tool for project development. Major goals of maven are as given below:

1. Build tool

2. Project management tool

We can use this maven tool when we are developing big projects with frameworks. When we are going for big projects then we face following common problems:

1. Multiple jars:
When we use frameworks like spring, struts etc. the we have to import multiple jar files. These jar files are nothing bu supporting files for our framework. In order to import these jar files we must know what jar files we have to import and how to bundle them.
This problem can be solved using maven tool. Maven fetches all related jar files from online repository or may be locally if you are doing this second time. For maven you just have to specify the project model that you want like spring, struts etc.

2. Jar dependencies:
It is related problem as above. When we import jar file then these jar files have some dependencies on each other. So we have to take care that we are importing these files in sequence.
This problem is also solved by the maven.

3. Project structure:
Nearly every frame work need some folder structure for project. Like main project files will be separate from test project files.
This is also done by maven. Maven creates directory structure according to our framework.

4. Build life cycle:
Maven takes care of build life cycle. This build life cycle contains number of phases like validate, compile, test, package and install. Some times when you have to make your code available to others the deploying that on repository comes into picture.

Maven fetches all jar file from its online repository. Maven maintains its local repository separately. Maven always looks into local directory before going for online. If you are doing some process for second time then maven will take files from local repository.

Now I am assuming that you have maven installed on your machine.
I will be adding post for installation as well.
In order to test maven installation you can type maven on command prompt.

Here I am explaining about creating sample project using maven on windows 7 platform.

1. Now create one separate folder for maven test project.
2. Go to that folder from command prompt.
3. Execute command "maven genapp".
4. Now it will ask you about project template. You can start with default so type default over there.
5. It will ask for id which is  top level component in your project.
6. It will ask for Application name and package name.
7. After completing this process check out your maven project folder it will be running with lot many things. Here is project.xml is important file open that file check out details.
8. Now execute "maven eclipse". It will create eclipse project for you.
9. Open eclipse and go to windows=>preferences in new window go to java=>build path=> class path variable. Here add new variable named "MAVEN_REPO" and give pathe of maven repository folder.
10. Now import this project to your eclipse from file=>import option.
11. Run it and you will get all .class file in your target folder.

If you want to clean every thing except java code execute "maven clean" command.

You can find out more details at http://maven.apache.org/  
I will try to add one video tutorial for this one for better understanding.