Showing posts with label J2EE Stuffs. Show all posts
Showing posts with label J2EE Stuffs. Show all posts

Wednesday, May 07, 2008

Make your JAVA application as executable jar file.

1. get the class file for the source code. create package structure for the class file.
In our example i have created a java file with a package structure as com.oracle.samples.HELLOWORLD.java.
Source Code for HELLOWORLD.java file.

package com.oracle.samples;

public class HELLOWORLD
{
public HELLOWORLD()
{
System.out.println("Iam printing from the HELLOWORLD constructor");
}

public static void main(String[] args) {
HELLOWORLD h = new HELLOWORLD();
}

}


D:\jdevstudio10133\jdk\bin\jar cvf HELLOWORLD.jar com\oracle\samples\HELLOWORLD.class

2. Create an empty folder and name it as META-INF.

3. Create a txt file and open that file in an editor. add the following line to it. Once added that line then hit the enter to get an empty line.
The empty line is very important, otherwise your manifest addition will not work.

main-class: com.oracle.samples.HELLOWORLD

D:\jdevstudio10133\jdk\bin\jar umf manifest.txt HELLOWORLD.jar

4. Once you done with the above steps you are ready to run the executable jar file.

D:\jdevstudio10133\jdk\bin\java -jar HELLOWORLD.jar

Note: If you want to add some jar files for your application you need to follow the steps below.
Create a folder called "lib" drop the your jar files under the folder example: log4j-1.2.13.jar or ojdbc14.jar
In the manifest.txt file you have to add one more line to it, add the below line.
Class-Path: lib\ojdbc14.jar lib\log4j-1.2.13.jar
After entering the above in the manifest.txt file you have hit the enter to have a empty line. This is very important otherwise you will get exception.

If you want to add properties file, you have to follow the below steps.
You can add the properties file inside the jar file or you can keep outside the jar file.

Friday, April 04, 2008

Simple way to implement Log4j in your applications

a. Download the log4j.jar file from the following web page.
http://logging.apache.org/log4j/docs/download.html
b. I have used apache-log4j-1.2.15.jar version in my example.
c. Add this jar file into your java project.
d. Import the log4j classes(Logger) into your java classes.
org.apache.log4j.Logger;
e. i have created a logger class in which i have a static method which return the logger object. In that static method,it has a Hashtable object which will hold the logger object.If second time called, the method will check in the Hashtable and if any logger object available, it will be retured. otherwise it will create a new object and stored in the Hashtable object and return the Logger object created.
public static Logger getLogger(String strLogName)

{
objHashTable = new Hashtable();
Logger log = (Logger)objHashTable.get(strLogName);
if(log == null)
{
log = Logger.getLogger(strLogName);
objHashTable.put(strLogName, log);
}
return log;
}
f. The configuration in the log4j.properties is as follows.
#log4j.rootLogger=DEBUG, A1, A2log4j.logger.A1=ALL,A1log4j.logger.A2=ALL,A2
# This is to print on the Console# log4j.appender.A1=org.apache.log4j.ConsoleAppender
# This is to write to a Log File

log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.MaxBackupIndex=10
log4j.appender.A1.MaxFileSize=10MB
log4j.appender.A1.File=D:/OHIOLog.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d - %m%n# --Print the date in ISO 8601 format# log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#---- Below for A2 configuration# This is to write to a Log File

log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.MaxBackupIndex=10
log4j.appender.A2.MaxFileSize=10MB
log4j.appender.A2.File=D:/BadDatas.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout

log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d - %m%n# --Print the date in ISO 8601 format# log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
In this example iam using 2 different log files, one for logging the error and debug messages and the other for collecting the bad datas which cannot be inserted into the table with any of thefollowing failures.
1. Primary Key constraints2. FK constrains3. Null values
Iam capturing the entire sql statement if any error occured during insert. In this example iam using 2 files A1 and A2.A1 will capture the debug and error messages.
In my java classes i will be using like the folloing statement.
try{

String sPersonInsert = "Insert into persons(person_id, name)values(iPersonId,sName)";
// Example of debug message logging into log file.

UnivLogger.getLogger("A1").debug("Database connection established ");
}catch(SQLException eSQLException)

{
// Example of error message logging into log file.
UnivLogger.getLogger("A2").error("The failed insert SQL statement is ==>"+sPersonInsert);
}
If you have any question, please update this post with your comments, i will get back to you with answers. Happy Coding.

If you want to print only the log message, with out any time stamp in it, in use the following conversion pattern
log4j.appender.A2.layout.ConversionPattern=%m%n