Problem using MySQL JDBC on Tomcat 5.5 and Ubuntu 8.04

, , , No Comments »

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

Signing Java Applets Using RSA Certificate

No Comments »
  1. Generate a private/public key pair with the RSA algorithm.
    $ keytool -genkey -keyalg rsa -alias keyname
    Enter keystore password: password
    What is your first and last name?
    [Unknown]:
    What is the name of your organizational unit?
    [Unknown]:
    What is the name of your organization?
    [Unknown]:
    What is the name of your City or Locality?
    [Unknown]:
    What is the name of your State or Province?
    [Unknown]:
    What is the two-letter country code for this unit?
    [Unknown]:
    Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
    [no]:

    Enter key password for <keyname>
    (RETURN if same as keystore password):

  2. Generate the self-signed certificate containing the public key, valid for one year.
    $ keytool -selfcert -alias keyname -validity 365
    Enter keystore password: password
  3. Use jarsigner to sign the JAR file, using the RSA credentials in your keystore that were generated in the previous steps. Make sure the same alias name is specified.
    $ jarsigner MyApplet.jar keyname
    Enter Passphrase for keystore: password
  4. Verify the JAR file.
    $ jarsigner -verify -verbose -certs MyApplet.jar

    s = signature was verified
    m = entry is listed in manifest
    k = at least one certificate was found in keystore
    i = at least one certificate was found in identity scope

    jar verified.

  5. Your applet has been signed properly. You are now ready to deploy your RSA signed applet.

Setting Up Log4j Quickstart Guide

No Comments »

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

Setting Default JVM in Ubuntu

, No Comments »

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.

Java Code Snippet for Sending Email using Gmail SMTP Server

No Comments »

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);
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in