Archives for Applet Category\

Signing Java Applications and Solving permissions denied problems

Thursday, July 2nd, 2009

Today i explain how you sign Java Applications.
Some times you need develop application that need write or read file in other computer’s through applet.
And you receive in your java console this message:

java.security.AccessControlException: access denied (java.io.FilePermission …

For you make this you need sign your applet.

  • First you need the certificate “Code Signed”. You need buy him.

Now you need list the key’s contents in your certificate:

keytool -list -storetype pkcs12 -keystore /path/to/your/certificate.p12

For signing your applet make this command:

jarsigner -storetype pkcs12 -keystore /path/to/your/certificate.p12 yourJar.jar "Alias"

Being “Alias” your alias returned in command keytool -list.

Now, if you need, this command verify if you applet is signed:

jarsigner -verify -verbose -certs yourjar.jar

Ok, your Applet is signed, but some times this isn’t enough. Java console continued displayed the error message.

Some cases in if you call the void’s or function’s of applet by Javascript the permission error continued.

For solving, use in your code that dispatch the exception this code:

AccessController.doPrivileged(new PrivilegedAction() {
	public Object run() {
		// YOUR CODE HERE
	return null;
});

Sample how to use:

public void PrintReport() {
	AccessController.doPrivileged(new PrivilegedAction() {
		public Object run() {
			FileOutputStream fos = null;
			PrintStream ps = null;
			try{
				fos = new FileOutputStream("LPT1:");
				ps = new PrintStream(fos);
				ps.println("print line");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			ps.close();
			return null;
		}
	});
}

Is this, i hope help you.
Bye.