Wednesday, February 27, 2008

Verisign signing - Java ME

Verisign signing - Java ME

A commercial application which requires access to some specific APIs like JSR 75, JSR 205, JSR 135 etc. benefits from getting signed with a commercial certificate from third party certificate providers like VeriSign or Thawte. Before buying any certificate from these third parties, you have to make sure that the target phone's of your device has the corresponding root certificate installed and set to allow MIDlet signing. You can check this from the settings of the target mobile device.

The following paragraphs contain the description of the process and steps required to get a certificate from VeriSign and to sign & test you MIDlet.

Getting the certificate

Step 1

Make sure, you have the latest JDK installed on your computer.

Step 2

Create a keystore

You have to generate a public/private key pair, for this enter the following command, specifying a name for your keystore and an alias as well.

 C:\jdk1.3\bin\keytool -genkey -keyalg rsa -keystore 
-alias

Keytool prompts you to enter a password for your keystore, your name, organization, and address. The public/private key pair generated by keytool is saved to your keystore and will be used to sign J2ME applications. This key is never sent to VeriSign and is required to sign code. So you should make a copy of the public/private key pair and store it in a safe deposit box or other secure location. If the key is lost or stolen, contact VeriSign immediately to have it revoked.

Step 3

Create a CSR

You need to generate a Certificate Signing Request (CSR) for the enrollment process.

1. The following command requests Keytool to create a CSR for the key pair in the keystore:

 C:\jdk1.3\bin\keytool -certreq -file certreq.csr -keystore 
-alias

2. Begin the enrollment process for a Code Signing ID from the products and services section of the VeriSign Web site.

3. Copy the contents of the CSR and paste them directly into the VeriSign enrollment form. Open the file in a text editor that does not add extra characters (Notepad or Vi are recommended).

This is the end of requesting the certificates from VeriSign.

Signing the MIDlet

Step 1

After VeriSign has verified your request, they will send an email with the digital id attached. You have to import it to the keystore.

To import your Sun Java Signing Code Signing Digital ID into your keystore, enter the following code with the path correct name for your file (for example, "cert.cer") to your Code Signing Digital ID.

 C:\jdk1.3\bin\keytool -import -trustcacerts -keystore 
-alias -file cert.cer

Step 2

Now sign the application by the two command prompt

 java -jar JadTool.jar [ -addcert -alias keyAlias [ -keystore keystore ]
[ -storepass password ]-inputjad inputJadFile -outputjad outputJadFile ]

java -jar JadTool.jar [ -addjarsig [ -jarfile jarFile ] -alias keyAlias
[ -keystore keystore ]-storepass password -keypass keyPassword
-inputjad inputJadFile -outputjad outputJadFile ]

For all the signing steps, Courtesy:

Monday, February 25, 2008

Reading an image from Gallery in Java ME

How to read an image from Gallery in Java ME

This article shows, how to read an image from Gallery by using FileConnection API (JSR-75) and show it on Form. The Gallery folders can be accessed by using system properties, or example: System.getProperty("fileconn.dir.photos"). File contents are read by using InputStream, data is saved to a byte array and Image object is created by using Image.createImage() method.

The full source code for a test MIDlet:

ReadImage.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;

public class ReadImage extends MIDlet implements CommandListener
{
private Form form;
private
String photos = "fileconn.dir.photos";
private
Image image;
private Command exitCommand;

public
void startApp() {
form = new Form
("ReadMIDlet");
exitCommand = new Command
("Exit", Command.EXIT, 1);
form.
addCommand(exitCommand);
form.
setCommandListener(this);
Display.
getDisplay(this).setCurrent(form);
String path = System.getProperty(photos);
form.
append(readFile(path + "image.png"));
}

public
void pauseApp() {
}

public
void destroyApp(boolean unconditional) {
}

public
Image readFile(String path) {
try
{
FileConnection fc =
(FileConnection)Connector.open(path, Connector.READ);
if(!fc.exists()) {
System.out.println("File doesn't exist!");
}
else {
int size = (int)fc.fileSize();
InputStream is = fc.openInputStream();
byte bytes[] = new byte[size];
int length = is.read(bytes, 0, size);
image =
Image.createImage(bytes, 0, size);
}

} catch (IOException ioe) {
System.out.println("IOException: "+ioe.getMessage());
} catch (IllegalArgumentException iae) {
System.out.println("IllegalArgumentException: "+iae.getMessage());
}
return image;
}

public
void commandAction(Command c, Displayable d) {
if (c == exitCommand) this.notifyDestroyed();
}
}

Code Optimization - Java ME

Code Optimization - Java ME

Since mobile phones have low heap memory, restrictions on jar sizes supported, and slow CPU's, optimizations can be employed to improve your applications performance. Consider your usage of memory, application size, and CPU overhead.

Here is a compilation of code optimization techniques which may be quite helpful to reduce jar size and memory usage.

1. Use an obfuscator like the free Proguard or Retroguard. They can remove unused code and perform other optimizations to decrease MIDlet size (JAR file size).
2. Use minimum number of classes, even a mostly-empty class uses space.
3. Avoid unnecessary object creation.
4. Reuse objects when possible.
5. For saving memory, explicitly set unused object to null.
6. Beware of calling garbage collection, calling system.gc to free memory may cause slowdowns because in certain phones it is a blocking statement.
7. Use only minimum images in the application to save space.
8. If not using an obfuscator
1. All the variable, method & class name should be small
2. Remove any unwanted code
3. The package structure should be as minimum as possible, if possible avoid packages