Tuesday, February 16, 2010

How to create library jar file and use that jar in another project

Sometimes we use some codes/classes redundantly in many application without any changes in the code.
In such type of cases its better to create a jar file with those classes and use only the jar file on other projects.
For exmaple, if we have some classes with custom UI , then we can create a jar file with the classes rather adding all the classes in other projects repeatdely also in those case we have to change the packge name based on the directory.

First , How to Create the Jar file
1. Create a new workspace (for example lib.jdw)
2. Create a new project(for example lib.jdp) under lib.jdw
3. Add the classes you want to create jar.
4. Note that no main is reqired.
5. From project properties make the project as library.
    Right Click on the Project -> Properties.. -> Application Tab -> Select Project type as Library
6. Build the project
7. Now you should have a jar file named lib.jar in the project file directory.
8. Preverify the jar file
    a. Go to your JDE installation folder (C:\Program Files\Research In Motion\BlackBerry JDE 
    b. Copy lib.jar file to bin folder.
    c. Now open your command prompt and change your current directory to your JDE installation 

    d. Now execute the following command:

        preverify -classpath "C:\Program Files\Research In Motion\BlackBerry JDE 4.6.0\lib 
        \net_rim_api.jar"  "lib.jar"
    e. In your bin directory(C:\Program Files\Research In Motion\BlackBerry JDE 4.6.0\bin) a folder  
        named output will create.
    f. Preverified jar file resides here.

9. Now rename lib.jar as lib.zip and delete cod , csl , cso file from there
10. Rename it back to lib.jar (make sure cod,csl,cso has been deleted)
11. Now you have got the jar file that you can reuse.

Second, How to add this jar to other project
1. For example, yow want to add this jar from another project (different workspace) app.jdp.

2. Import the jar file from the project app.jdp 
    Right Click on the Project -> Properties.. -> Build Tab -> Add lib.jar as Imported Jar    files:
3. Now you will be able to import the classed of lib into the new project app.