Thursday, March 12, 2009

How to Import kxml jar File to Your Project

This is my first technical write-up. I never felt like writing this sort of thing before. I've been working on BlackBerry application development for the last couple of months and faced a number of problems some of whose solutions were not easy to find out and required more effort and time than anticipated. So I thought that it would be a good idea to write down the solutions to problems that I faced.

Of late I had difficult time in XML parsing with BlackBerry API. The API worked fine for version >=4.3, even for version 4.2.1, but frustratingly failed for version which I tested with 8300-Vodafone simulator. I searched frantically for a solution to this exasperating problem but in vain. So I had to look for an alternative.

Here came the great kXML parser. I downloaded the jar file.

Importing a jar file to my project in JDE at first seemed an easy task. A little bit search led to my project's Properties -> Build and then Add to imported jar files. But this procedure failed miserably and showed -

Searching installed codfiles for alias 'kxml2-2.3.0'
Searching uninstalled codfiles for alias 'kxml2-2.3.0'
Module 'kxml2-2.3.0' not found.Error starting kXmlParserTest: Module 'kxml2-2.3.0' not found.

Here, kXmlParserTest is my workspace name.

Googling led me to the following workaround:

1. Create a different project, say kXmlProject.

2. Add the kXML jar file to kXmlProject through Add File to Project.

3. Go to kXmlProject's Properties -> Application and select Library in Project type option.

4. Now build the project

But the project build failed and showed : Error!: Missing stack map at label: 79
Error while building project

Again I resorted to googling and someone at one of BlackBerry developer forums wrote that this problem occurs due to incompatible binary structure of jar file and suggested a solution - preverify the kXML jar file. But how?

1. Go to your JDE installation folder, for Windows XP it is usually C:\Program Files\Research In Motion\BlackBerry JDE 4.3.0\bin

2. Copy the kXML jar file to bin folder.

3. Notice the preverify.exe file. This is the tool we'll use.

4. Now open your command prompt and change your current directory to your JDE installation directory.

5. execute the following command:

preverify -classpath "C:\Program Files\Research In Motion\BlackBerry JDE 4.3.0\lib\net_rim_api.jar" "your_kXML_jar_filename"

6. Notice that in C:\Program Files\Research In Motion\BlackBerry JDE 4.3.0\bin directory, another folder named output has been created. Preverified jar file resides here. Copy the preverified jar file with the same name that the non-preverified jar file has.

7. Now replace the non-preverified jar file with the verified one. Remove the former from your kXmlProject and add the latter.

Then the project build was successful and this project was going to be used as a library for my other project, say kXmlParserTestProject.

I tried to build kXmlParserTestProject but project build failed. Then was the time for fixing project dependencies.

- Go to kXmlParserTestProjec's Project Dependencies and check kXmlProject.

Then everything worked fine. Don't stop reading here. Go on...

The day after I fixed this issue of jar file import spending a lot of time and effort, my colleague Bikas who was working with me in the same project, informed me that two cod files were created for two different projects. But single cod file was convenient for installation. While I was googling for a workaround, Bikas, just out of sheer curiosity whether it worked or not, removed the kXmlProject library project, removed project dependencies and added the preverified jar file as a normal java file to the project. This simply worked fine X-(

Useful links:

How To - Compile a jar file into a BlackBerry Library