Wednesday, August 1, 2012

Developing J2ME on eclipse m2e with Bouncycastle LW library


Q: java.lang.NoClassDefFoundError: java/security/SecureRandom: Cannot create class in system package
A:


  1. Make sure the source folders and output folders are set to "src" and "bin" respectively. This is important: my habit is to use the project folder for both source and class files, but your built packages will have errors if you do this. EclipseME seems to put all the contents of the "source" directory (which, if it's the package directory, includes things like "deployed" and ".settings" and other stuff) in your jar, which causes problems.
    • Go to: Project -> Java Build Path -> Source. Click "Add Folder", and select "src" (create it if you need to). Remove the project folder from the build path.
    • Next, select "projectFolder/bin" as the default output folder.
    • Go to Window -> Preferences -> J2ME. Set "bin/deployed" as the deployment directory.
  2. Install and set up the BouncyCastle crypto library.
    • Download the bouncycastle j2me files from http://www.bouncycastle.org/latestreleases.html. The easiest way is to download the complete package (named something like "crypto-139.tar.gz"). Expand the archive, and look for the file "cldcclasses.zip". This is the library the J2ME apps will use.
    • Add the clcd_classes.zip library to your project: Project -> Properties -> Java Build Path -> Libraries.
    • Be sure to check "cldc_classes.zip" under "Order and Export" in Project -> Properties -> Java Build Path. It must be built with your package for obfuscation, etc. to work.
  3. Set up obfuscation. BouncyCastle includes some classes that are reimplementations of system classes (such as java.security.SecureRandom and java.lang.BigInteger). You will receive runtime security errors if your application tries to add these classes to the system. To avoid this, it is necessary to obfuscate the classes (which renames them, and places them in the default package).
    • Install ProGuard. Note that EclipseME doesn't seem to work right with proguard from the debian/ubuntu package repository, you probably have to download it manually from http://proguard.sourceforge.net. Extract the archive, and set up the ProGuard preferences in eclipse.
    • Go to: Window -> Preferences -> J2ME -> Packaging -> Obfuscation. Under "Proguard Root Directory", put the root directory of the proguard files downloaded from sourceforge (it should contain "lib", "src", "examples", "docs", etc). While there, also check the box so that the specified arguments include "-dontusemixedcaseclassnames -dontnote -defaultpackage ''". Ensure that "Proguard Keep Expressions" includes "public class * extends javax.microedition.midlet.MIDlet".
  4. Now you are ready to write your crypto code! But note that your development process and debugging are now different. Because bouncycastle depends on obfuscation for the code to run at all, and obfuscation only runs during the "packaging" stage, you can no longer simply run your emulated MIDlet with the WTK emulator to debug. Instead, you must use the following steps to test your program:
    • Right-click on the project folder, and select "J2ME -> Create Obfuscated Package".
    • Select "Run" from the "Run" menu (the first time, you can't just do "Run last launched" - you need to edit the configuration). Check the "Jad URL" radio button, and put in the path to the built JAD file (project/bin/deployed/yourJad.jad). Finally, click "Run", and you can run your project. The emulator will start listing the applications present in your JAD/JAR, and you have to launch one to test it.
Source: http://tirl.org/blogs/media-lab-blog/46/


Q: could not find jar tool executable
A: Configure the default Java to a JDK instead of JRE

Source: http://jclik.wordpress.com/2009/10/28/eclipse-could-not-find-jar-tool-executable-solution/