How do I use Ant (bb-ant-tools) to compile a JAR file into a COD? i.e. with no other source files
Ultimately, I need a script
From your console output, the command that is failing:
jar -cfmv C:\development\ant\new_test\MyApp\build\MyApp.jar C:\Users\Richard\AppData\Local\Temp\rapc_598c0c5a.dir\META-INF\MANIFEST.MF MyApp.cod MyApp-1.cod MyApp-2.cod MyApp.csl MyApp.cso -C C:\Users\Richard\AppData\Local\Temp\rapc_598c2ad7.dir .
Looking up those options in the jar tool documentation:
-C dir
Temporarily changes directories (cd dir) during execution of the jar command while processing the following inputfiles argument. Its operation is intended to be similar to the -C option of the UNIX tar utility.
Based on that, I think rapc is putting the unpacked cod file in C:\Users\Richard\AppData\Local\Temp\rapc_598c2ad7.dir and that is causing conflicts with the cod files specified on the command line. Is that directory still around? Look inside to see what's there.
To answer my own question with some details...
One must not call rapc
multiple times - it will create too many COD files. That is why I was getting that error.
Following up from Michael's answer, the correct way to proceed is to build the final JAR file using the normal java tools (javac & jar) as well as RIM's preverify
command.
Only use rapc
for the last step - converting that JAR file into a COD.
A full ANT build framework to deal with this problem is too big to place here, but the steps needed to create it are listed below. Each of the steps can be easily researched on this site (or with some google). Each step is very simple, and can be debugged individually.
Steps:
javac
the SDK to create CLASS filespreverify
the CLASS filesjar
the SDKjavac
the project - use the SDK JAR as the classpathpreverify
the project CLASS files (again, use the SDK JAR in the classpath)jar
the project - add the SDK JAR as a zipfilesetjarjar
this project JAR to refactor package names as requiredrapc
on this JAR - it will find no duplicate COD files & should run fine.Note: Steps 1-3 can be combined by just using rapc
on the SDK (which is needed if you need to run preprocessor tags on the SDK code).
By breaking it down into simple steps like this, I learnt how the normal java tools link into RIM's toolchain (normally this is all hidden when you simply invoke rapc
on a source folder).
Of course, you still need to sign the COD with the sigtool
.
I do all of this in ANT. I use a different folder to store the output from each step as I go. This way I end up with 5 temp folders at the end, but it made it easy to debug the steps as I go.
I finally understand now why so few people were able to offer conclusive answers to my various BB ANT build script questions. The process is laborious and very long, and hard to explain.
A full ANT build framework to accomplish this can stretch over many different files (in my case I think I'm now using 8 including property files). And it requires a good working knowledge of ANT, normal java build tools, and RIM's rapc
command.
I think I have documented each step of the process quite well in my questions about this, and picked up some great answers along the line. For more details, have a look at those other questions & answers. Each one contains useful links, and some good insight from the other developers in this community
Copying this answer from BlackBerry - Ant script to include JAR in project without external dependancies as per @RichardLeMesurier's request:
I had a similar problem last year, I had created a "framework" that was used as a base for multiple BB-applications, but run into problems with multiple CODs (I don't remember exactly what, something like the device refused to install multiple applications that had same external cods, if the external CODs weren't installed separately first, and then the applications). As the applications could be installed separately (one person might install only app A, another might install only app B, and yet another might install both A and B), the whole framework needed to be included in all the apps using it. I cooked up this Ant-script using bb-ant-tools (hopefully I didn't break anything removing some stuff specific to our apps and obfuscating package names etc):
<?xml version="1.0" encoding="UTF-8"?>
<project name="${description}" default="build" basedir=".">
<taskdef resource="bb-ant-defs.xml" classpath="lib/bb-ant-tools.jar" />
<!-- rapc and sigtool require the jde.home property to be set -->
<!-- NOTE: You may need to copy the signature files from Eclipse\plugins\net.rim.ejde\vmTools to the components\bin -dir
if the keys were installed using the Eclipse-plugin -->
<property name="jdehome" value="C:\BB\Eclipse\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components" />
<!-- Framework source locations, these must be set correctly -->
<property name="frameworkRes.dir" value="C:\BB\workspace\BB_Framework\res" />
<property name="frameworkSrc.dir" value="C:\BB\workspace\BB_Framework\src\com\whatever\frame" />
<!-- Locations for simulator, binaries, jde home, don't touch these -->
<property name="simulator" value="${jdehome}\simulator" />
<property name="bin" value="${jdehome}\bin" />
<property name="jde.home" location="${jdehome}" />
<!-- directory of simulator to copy files to -->
<property name="simulator.home" location="${simulator}" />
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="temp.dir" location="C:\tempsrc" />
<!-- Project specific -->
<!-- Application title -->
<property name="app.title" value="Application Name" />
<property name="app.version" value="1.0.0" />
<!-- Value to prepend before frame-class packages -->
<property name="frame.prefix" value="appname" />
<!-- Name of the COD to produce -->
<property name="cod.name" value="Appname" />
<target name="build">
<mkdir dir="${build.dir}" />
<delete dir="${temp.dir}" />
<mkdir dir="${temp.dir}" />
<mkdir dir="${temp.dir}\${frame.prefix}" />
<copy toDir="${temp.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</copy>
<copy toDir="${temp.dir}\${frame.prefix}">
<fileset dir="${frameworkSrc.dir}">
<include name="**/*.java" />
</fileset>
</copy>
<copy toDir="${temp.dir}\res">
<fileset dir="${frameworkRes.dir}">
<include name="**/*" />
</fileset>
</copy>
<copy toDir="${temp.dir}\res">
<fileset dir="res">
<include name="**/*" />
</fileset>
</copy>
<!-- This replaces the package names for classes copied from under
framework-directory to ${frame.prefix} -directory as well as changing any
imports using the classes in framework-package -->
<replace dir="${temp.dir}" value="${frame.prefix}">
<include name="**/*.java"/>
<replacetoken>com.whatever.frame</replacetoken>
</replace>
<rapc output="${cod.name}" srcdir="${temp.dir}" destdir="${build.dir}">
<jdp title="${app.title}"
version="${app.version}"
vendor="Your Company"
icon="../res/img/icon.png"
/>
</rapc>
</target>
<target name="sign">
<sigtool codfile="${build.dir}/${cod.name}.cod" />
</target>
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="load-simulator" depends="build">
<copy todir="${simulator.home}">
<fileset dir="${build.dir}" includes="*.cod,*.cso,*.debug,*.jad,*.jar" />
</copy>
</target>
</project>
What this does, is copy all the java-files and resources from your current project and then from the framework-project to a temporary directory, replacing package names on the way for the frame-work files (as they're put into a separately named directory), this has to do with the fact that the devices also refused to install multiple applications that had same classes under same packages (namely, the framework classes, for your case this might not be necessary). After the copying and replacing is done, the application is built to target build-directory using rapc. There are separate tasks for signing, cleaning and loading the app to a simulator. Hope this helps.