- 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): - Generate the self-signed certificate containing the public key, valid for one year.
$ keytool -selfcert -alias keyname -validity 365
Enter keystore password: password - 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 - Verify the JAR file.
$ jarsigner -verify -verbose -certs MyApplet.jars = 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 scopejar verified.
- Your applet has been signed properly. You are now ready to deploy your RSA signed applet.
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.%tOutput the thread that generated the logging event.%-5pOutput the priority of the logging event left
justified to a width of five characters.%COutput the fully qualified class name of the caller
issuing the logging request.%mOutput the application supplied message associated with
the logging event.%nOutput the platform dependent line separator character or
characters. e.g “\n” or “\n\r”
For more information refer to the Log4j documentation
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.
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);
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
String input = "2007-05-04";
Date d;
try {
d = formatter.parse(input);
}
catch (ParseException e) {
e.printStackTrace();
}