Monday, January 28, 2008

Getting your J2ME project working with Netbeans Mobility Pack and Bouncy Castle Lightweight API

This is just a quick way to get started with BC API and ECC. It is not the best or perfect way.

  1. Visit Bouncy Castle Download Page. Download Lightweight API for J2ME zip file. Search for Lightweight API or J2ME on the page to find the exact file.
  2. Unzip the file to say folder /BouncyCastle.
  3. Create a Mobile Application Project in Netbeans.
  4. Go to Project Properties -> Build -> Libraries and Resources ->Click on Add Jar/Zip
  5. Add BouncyCastle/lightwt....j2me/zips/cldc_classes.zip
  6. Go to Project Properties -> Build -> Obfuscation -> Select High Obfuscation Level and add following to Additional Obfuscation Settings.

-dontskipnonpubliclibraryclasses

-dontskipnonpubliclibraryclassmembers

-ignorewarnings

12 comments:

fizk said...

I followed your steps, but I get this error:

Uncaught exception java/lang/NoClassDefFoundError: org/bouncycastle/asn1/ASN1InputStream: Cannot create class in system package .

-----------
Code:
-----------
ByteArrayInputStream is = new ByteArrayInputStream(privkey); // Decoded PEM Key plain
org.bouncycastle.asn1.ASN1InputStream asn1is = new org.bouncycastle.asn1.ASN1InputStream(is);
DERObject obj = asn1is.readObject();
is.close();

Himanshu said...

not sure...it seems you are facing a java classpath issue. when you are running the code, for some reason, the BouncyCastle classes cannot be found.

fizk said...

For some reason it works in NetBeans (Windows Vista), but not in Eclipse (Ubuntu)....

I've had such a hard time finding RSA encryption/decryption with already existing key files on j2me. After a few days, I got it working last nite.

If you're interested, I'd like to post the code on your blog :)

Himanshu said...

haha.....please post it as a comment and I will approve it....

Unknown said...

can you please post the code for j2me encryption/decryption on already existing key files?

Unknown said...

Can you please let me know how you solved the issue of java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1InputStream: Cannot create class in system package ?

I can't seem to figure out why my code compiles fine in my Eclipse IDE but at runtime gives that error. I will really appreciate your help.

Himanshu said...

Taji, for code on encryption/decryption using J2ME, you should be able to find several sample examples in the Lightweight API. I think there is even a sample Midlet. I do not have the code on my machine anymore. But if you still are not able to locate it, let me know and I will try to retrieve the code.

The runtime error you are facing is obvioously as you know an issue with the classpath. Is this happening before or after obfuscation? Did you add cldc_classes.zip in your classpath. Also, verify that cldc_classes.zip is the classpath of the run configuration.

Unknown said...

I am using Eclipse GANYMEDE and jdk 1.5 with Proguard 4.4

I have cldc_classes.jar and cldc_crypto.jar in my Eclipse build path path. When I run "Create Obfuscated Package" it compiles without a problem. It's when I run the Emulator that I get that error. Can't seem to see what's wrong with my setup. Do you mind digging up your code?
Thanks

Unknown said...

Himanshu,

Can you please help me with issue on how you were able to solve this classpath issue using Eclipse. I've tried everything possible but still having the same issue. Even searched the web but no answers. I will really really appreciate your help. Thanks.

Himanshu said...

Hey Taji, Can you upload your eclipse project somewhere. If I have time I could take a look. Also, I personally preferred Netbeans for J2ME development, mainly because it provided a GUI Widget palette and very good integration with tools such as the application deployment tool for testing by DeviceAnywhere (http://www.deviceanywhere.com).

Unknown said...

Himanshu, you can download my eclipseproject from here:

http://simpaq.com/archives/SMSEncryption.zip

Anonymous said...

haloo....
I need cldc_classes.zip
please sent to my email
andriyudha@ymail.com
thank you very much,,