Nov 04
Normally when I build a web application that connects to a MySQL database, I would place the mysql-connector-java-bin.jar file into the WEB-INF/lib of the web application.
However, when I deployed the application on Tomcat 5.5 on Ubuntu 8.04 I got an exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
I couldn’t find the reason which caused the exception, so I did a quick fix by placing the jar file into the Tomcat’s common lib directory.
$ cp mysql-connector-java-bin.jar /usr/share/java
$ cd /usr/share/tomcat5.5/common/lib
$ ln -s ../../../java/mysql-connector-java-bin.jar mysql-connector-java-bin.jar
Jun 13
Log4j is an open source tool developed for putting log statements into your Java application. It’s allows log statements to remain in shipped code while giving the user the ability to enable logging at runtime without modifying any of the application binary.
The easiest way to setup Log4j is to create a log4j.properties file and put in root of your classes directory. A typical example of the log4j.properties file is as follows:
# Set root logger level to INFO and log to console, and log file appender.
log4j.rootLogger=INFO, Console, LogFile
# Set the logger level to DEBUG for all classes under the package com.mycompany.
log4j.logger.com.mycompany=DEBUG
# Define the settings for the console appender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %C - %m%n
# Define the settings for the log file appender.
# Log file rotates every month.
log4j.appender.LogFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LogFile.DatePattern='.'yyyy-MM
# Location to where the log file exists.
log4j.appender.LogFile.file=/path/to/logfile.log
log4j.appender.LogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.LogFile.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %C - %m%n
The ConversionPattern determines what the format of each line logged:
%d{ISO8601} Output the date in ISO8601 format.
%t Output the thread that generated the logging event.
%-5p Output the priority of the logging event left
justified to a width of five characters.
%C Output the fully qualified class name of the caller
issuing the logging request.
%m Output the application supplied message associated with
the logging event.
%n Output the platform dependent line separator character or
characters. e.g “\n” or “\n\r”
For more information refer to the Log4j documentation
Jun 11
There are circumstances where you may need to have more than one version of Java installed, but how do you switch between them?
Execute the following command to list the installed JVMs:
$ sudo update-alternatives --config java
Simply enter the number for the JVM to use. The * shows the current selected JVM.
Jun 07
The partial code below shows how to sent an email using the Gmail’s SMTP server:
Properties properites = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username@gmail.com", "password");
}
});
Message msg = new MimeMessage(session);
msg.setFrom(...);
msg.addRecipient(...);
msg.setSubject(...);
msg.setText(...);
Transport.sent(msg);