Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Tutorial: How To Use 3rd Party Libraries in your Applications

[ Edited ]

Hi,

 

I've been seeing a lot of requests involving people trying to get 3rd party libraries to work with their applications. People are commonly referred to this page for fixing it. But it requires the use of the BB JDE and if you're like me it doesn't work anyways.

 

These steps will help you build the 3rd party libraries so they work on the BlackBerry. This requires bb-ant-tools because I couldn't get a manual compile to work! There's a great tutorial posted by tadwoods on installing and configuring bb-ant-tools here.

 

You must also have a component pack installed on your computer. i.e. Have the JDE installed OR one of the eclipse plugin component packs.

 

  • Download the third party jar that you require for your BlackBerry application.
    • 3rd party jars MUST be written for either J2ME CLDC1.1/MIDP 2.0 or the RIM API.
  • Preverify the jar file.
  • Create a project with that just holds the preverified jar and the bb-ant-tools jar.
  • Write a build.xml file that runs rapc on the preverified jar.
  • Copy all generated cod, cso, debug, and csl files to the simulator directory (included in the build.xml).
  • And of course, set your main application project to be dependent on the 3rd party jar so you can have access to it's API's.

 

That's all you have to do. Let's break it down some more though.

 

Preverify

 

The key is to preverify your 3rd party jar. Here's the syntax, create a .bat file or run it from the command line:

 

JDE_HOME/bin/preverify -classpath "JDE_HOME/lib/net_rim_api.jar" "3rdParty.jar"

 

Where JDE_HOME is the BB JDE home directory or the component pack home directory.

 

VERY IMPORTANT!!! The preverify command will output the results in a folder called "output". You must use the JAR file found there, as it is the one that has actually been preverified. The preverify tool does not override your third party jar.

 

I have heard that sometimes people have trouble with this step. I didn't... But I have read somewhere that you can try to unjar the class files, run preverify, then rejar the class files.

 

Create Project

 

This is pretty simple. Just load up your favourite IDE and create a new project. Within the project create a lib folder and copy the preverified jar into the folder. Create the build.xml. That's it.

 

Build.xml

 

Here's the real brains behind everything. bb-ant-tools is a magic worker, considering I can't even build a file manually using the same commands it uses (I must be missing a flag or something). Just set the rapc command in the build.xml and configure the jdp element. You don't even need to have a special BlackBerry plugin installed for your IDE! See next post for example build.xml file (too big for one post).

 

Simulator

 

Build your main application however you already do it. Make sure you've already run the build.xml file for your 3rd Party library. Now just run the simulator in JDE_HOME/simulator/model#.bat and it should work.

Message Edited by cowkingcoder on 03-03-2009 12:50 PM
Message Edited by cowkingcoder on 03-03-2009 12:51 PM
Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

<?xml version="1.0" encoding="UTF-8"?> <project name="TheName" default="deploy" basedir="."> <!-- for rapc, jdp, sigtool, etc... commands. See: http://bb-ant-tools.sourceforge.net/docs for documentation on bb-ant-tools. Make sure project lib directory contains the bb-ant-tools.jar --> <taskdef resource="bb-ant-defs.xml" classpath="lib/bb-ant-tools.jar" /> <!-- Unfortunately, this directory pretty much has to be hard coded. You'll have to change it for your system. Just be sure it points to the RIM components root directory. Like the Eclipse Component pack plugin directory or the BB JDE install directory. There will be a bin directory inside the directory this points to. This directory is required so the bb-ant-tools can find the RIM binaries like rapc, sigtool, etc... --> <property name="jdehome" value="YOUR_JDE_HOME" /> <property name="simulator" value="${jdehome}\simulator" /> <property name="bin" value="${jdehome}\bin" /> <target name="deploy" depends="buildRIM" description="Builds and Deploys Project (installs to simulator)"> <copy todir="${simulator}" overwrite="true"> <fileset dir="dist"> <include name="*.cod" /> <include name="*.debug" /> <include name="*.csl" /> <include name="*.cso" /> </fileset> </copy> </target> <target name="clean" description="cleans the dist directory"> <delete dir="dist"/> <mkdir dir="dist"/> </target> <target name="buildRIM" depends="clean" description="Builds Project"> <!-- JAVA_HOME environment variable required. --> <rapc jdehome="${jdehome}" jdkhome="${java.home}" destdir="dist" output="CodName" quiet="false"> <jdp type="library" title="title" vendor="vendor" version="1.0" description="description" arguments="" systemmodule="false" runonstartup="false" startuptier="7" ribbonposition="0" icon=""> </jdp> <src> <fileset dir="."> <include name="lib/3rdParty-Preverified.jar" /> </fileset> </src> </rapc> </target> </project>

 

Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

If I ever figure out how to do this manually (without bb-ant-tools) I'll post it here. If you know, I'd appreciate you adding to the collective knowledge.

 

I think my problems are occurring on my rapc command. When I build it manually, I get a different sized cod file than when bb-ant-tools does it. Also, when I attempt to launch an application that utilizes the manually built cod, I get a "Symbol class.method not found" error. I suspect it's just some secret flag on the rapc command to tell it it's building a library or something. Here's the command I'm trying to use:

 

rapc import="JDE_HOME/lib/net_rim_api.jar" codename=3rdParty "3rdParty-preverified.jar"

 

Thanks! Even if you know a good link to documentation on the rapc command I'd appreciate it. Seems pretty much impossible to find anything decent.

New Developer
Posts: 13
Registered: ‎08-15-2008
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

Thanks for the great info.

 

Since the jar ends up in a seperate project does that mean I have to deploy 2 CODs the end users device?

 

If so what is the best way to handle versioning?  For example if MyApp1 uses 3rdPartyJar version 1 but MyApp2 uses 3rdPartyJar version 2 isn't version 2 going to overwrite version 1 causing MyApp1 to break?

Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

tony,

 

Yes, you'll have to deploy two cod files to the end user. One for your main application and another for the 3rd Party jar dependency. There may be a way to include multiple cod files together like in the net_rim_api.jar file, but I don't know it (and would love to hear how if anyone knows). In any case, I know that in the .alx files you can tell it what cod's the application is dependent on. I'm sure there'd be a similar way to do it in the .jad files if you are doing OTA updating.

 

When you have two different apps requiring two different versions of the 3rd Party application, my suggestion would be to create a separate cod for each version. Just call the cod files something different. For example, 3rdParty-1.0.jar becomes 3rdParty-1.0.cod and 3rdParty-2.0.jar becomes 3rdParty-2.0.cod.

 

Ultimately, you'll eventually want to update the app to use the newer version of the 3rd Party library, so you only have to distribute one version of the 3rd party cod.

 

Hope this helps!

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

Some hopefully useful comments.

 

1) "There may be a way to include multiple cod files together"

Don't know of a way, I too would love to know!

 

2) "I'm sure there'd be a similar way to do it in the .jad files". 

The following line in a jad file seems to provide the dependency functionality for jads:

RIM-COD-Module-Dependencies: net_rim_cldc,net_rim_locationapi, ....

 

3) "my suggestion would be to create a separate cod for each version. Just call the cod files something different."

My experience is that this does not work.  The issue is there is only one class loader in the JVM, so it will only load one class with a specific name.  If you have multiple cods, then the class loader will attempt to load the classes from each, will find that the classes have the same name, and will discard one of them. 

 

4) "you'll eventually want to update the app to use the newer version of the 3rd Party library,"

Given 3), this might be sooner rather than later......

Developer
Posts: 136
Registered: ‎02-12-2009
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

hi cowkingcoder,

thanks for the tutorial. Actually i am trying to run my app using third-party jar using your tutorial. I am kind of not getting it. 

According to your tutorial i have:-

1) Created a simple project that has  a lib folder that contains third-party.jar an bb-ant-tools.jar and also build.xml(not in lib folder).

2)  I have build that build.xml file successfully generated all the files in the "dist" folder.

3) I have created a project that contains my app.

4) Now I cannot understand how do i link or depend my app to the Third-paty.jar ? I am using eclipse 3.4  .

 

Thanks.

_____________________________________________________________________

Thank the person who helped you by clicking Kudos! star near their post..
If you start a new thread, then resolve it using Accepted Solution button..
Leave solved threads for reviewing purposes - Start a new thread for new questions..
Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

[ Edited ]

The build.xml file will also generate a .jar file in your dist directory. Copy that jar file into your main app project (wherever, probably a lib directory), then configure your main app project to depend on that jar.

 

(For eclipse, this entails just right-clicking the copied jar file... Build Path->Add to Build Path)

Message Edited by cowkingcoder on 03-05-2009 11:55 AM
Developer
Posts: 211
Registered: ‎04-29-2009
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

Hi,

Thanks for the detailed help.

 

I wanted to know, how can I add ".zip" files into a Blackberry project? I am using Blackberry plugin for Eclipse.

Tried the same steps suggested above for zip files.

But it did not work.

 

Please help.

 

Thanks in advance. 

New Developer
Posts: 21
Registered: ‎05-30-2009
My Device: Not Specified

Re: Tutorial: How To Use 3rd Party Libraries in your Applications

cowkingworker, thanks for the useful tips.

 

What happens if I have 1 jar file (a.jar, for example) that has dependencies on other jar files (b.jar and c.jar)?  Do I need to preverify b.jar and c.jar first, then preverify a.jar, then copy the prevarified jars to my project?

 

Basically, I want to use the package jweather-0.2.5.jar (http://sourceforge.net/projects/jweather/) in my project.  I saw your post, installed ant-bb-tools, and am now at the stage of preverifying the jar but get the error below:

 

Error preverifying class net.sf.jweather.metar.MetarFetcher
    VERIFIER ERROR net/sf/jweather/metar/MetarFetcher.fetch(Ljava/lang/String;I)
Ljava/lang/String;:
Cannot find class org/apache/commons/httpclient/HttpMethod

 

I tried preverifying commons-httpclient-3.1.jar but get this error:

 

Error preverifying class org.apache.commons.httpclient.AutoCloseInputStream
    java/lang/NoClassDefFoundError: java/io/FilterInputStream

 

Thanks again.