Sunday, June 27, 2010

Debug BlackBerry Application Inside NetBeans

This post continues the my last post  about blackberry application development.

I added the ANT target to launch the blackberry JDWP debugger from these two articles: 1) and 2). I can launch the BlackBerry JDWP and simulator, but unfortunately the app did not stopped at the break point.
I googled a lot, but it seems none of the post tells you that debugging blackberry successfully inside the NetBeans IDE, I did not find any tutorial about using JDWP in blackberry development document either.
So I have to figure out by myself by using reverse engineering again.

First I set up the BlackBerry project in the JDE environment, and it launched the debugger successfully, can stop at the break point, and can single step;
then I go to the simulator folder of the BlackBerry ( in my pc is C:\Program Files\Research In Motion\BlackBerry JDE 4.5.0\simulator), I found there not only .cod file and .jad file, but also found
 .debug, .jar, and .alx file. I realize this might be the reason: you need to copy .debug files in the simulator! In my old build script, I only copied jad and cod files, did not copy .debug files into the simulator.

So I modified my ant build script, make sure it will copy all the rapc generated files into the simulator folder, which includes:
  - .jad and .jar file, I don't know why JDWP needs jar file, but it works any way;
  - debug file, like .debug and xxx-1.debug, xxx-2.debug, etc;
  - .cod file
  - .alx file

The ant script snippet look like this:
1.  ANT target: copy files.  Since I used bb-ant, this target means copy all the file contains the project name under \bbant folder to the simulator folder.

<target name="copyBBSimulator" if="do.rapc">
    <copy todir="${platform.home}/simulator" verbose="true">
             <fileset dir="${dist.dir}/bbant">
                <include name="**/${name}*"/>
            </fileset>
    </copy>
</target>


2. ANT target: launch blackerry debugger, this target will launch the blackberry JDWP remote server and blackberry simulator.

<target name="bb-debug" depends="copyBBSimulator" if="do.rapc">
    <delete file="${preprocessed.dir}/.timestamp"/>
    <parallel>
          <java jar="${platform.home}/bin/JDWP.jar" fork="true" dir="${platform.home}/bin">
            <jvmarg value="-Xmx128M"/>
          </java>

          <sequential>
              <sleep seconds="5"/>
              <antcall target="nbdebug"/>
         </sequential>
   </parallel>
</target>


At this time I use the new ant script and press the debu button in NetBeans, this time it works!!! It takes a longer time to launch the debugger and simualtor, but finally the app stops at the break point, and it allows me to step in and step out.
Another thing I like to mention is for the system outputs, you can only see them from the BlackBerry JDWP output window, you can not see them from the NetBeans output window.


For your reference, I post the whole build script and source code:
1. bb-build.xml

<target name="post-init">
          <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
          <condition property="jpda.port" value="8000">
              <isset property="do.rapc">
              </isset>
         </condition>
        
  </target>


   <target name="post-jar" if="do.rapc">
     <antcall target="bbbuild" />
   </target>

   <target name="run" depends="init,jar,bb-run,cldc-run" />
   <target name="debug" depends="init,jar, bb-debug, cldc-debug" />


   <target name="cldc-run" unless="do.rapc" >
       <nb-run jadfile="${dist.dir}/${dist.jad}" jarfile="${dist.dir}/${dist.jar}" jadurl="${dist.jad.url}" device="${platform.device}" platformhome="${platform.home}" platformtype="${platform.type}" execmethod="${run.method}" securitydomain="${evaluated.run.security.domain}" commandline="${platform.runcommandline}" classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" cmdoptions="${run.cmd.options}"/>
   </target>

   <target name="cldc-debug" unless="do.rapc">
       <delete file="${build.dir}/.timestamp"/>
                <parallel>
                    <nb-run debug="true" debugsuspend="true" debugserver="true" debuggeraddressproperty="jpda.port" platformtype="${platform.type}" platformhome="${platform.home}" device="${platform.device}" jadfile="${dist.dir}/${dist.jad}" jarfile="${dist.dir}/${dist.jar}" execmethod="${run.method}" securitydomain="${evaluated.run.security.domain}" commandline="${platform.debugcommandline}" classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" cmdoptions="${run.cmd.options}"/>
                    <sequential>
                        <sleep seconds="5"/>
                        <antcall target="nbdebug"/>
                    </sequential>
                </parallel>
   </target>

<target name="post-clean">
   <delete failonerror="false">
      <fileset dir="${platform.home}/simulator">
       <include name="**/${name}*">
       </include>
      </fileset>
   </delete>
</target>

<typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
  <target name="bbbuild" description="blackberry build" depends="init" if="do.rapc">
          <echo message="rapc build, dir=${dist.dir}"></echo>
          <mkdir dir="${dist.dir}/bbant" />
          <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
          <src>
             <fileset file="${dist.dir}/${dist.jar}" />
          </src>
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" runonstartup="false" >
          </jdp>
          </rapc>

        <!-- sigtool jdehome="C:Program FilesResearch In MotionBlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
        <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
            <application id="${app.title}">
               <codset>
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
                   </fileset>
               </codset>
           </application>
       </alx>
       <mkdir dir="${dist.dir}/bbant-final" />
       <jadtool input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
        <fileset dir="${dist.dir}/bbant" includes="*.cod">
        </fileset>
      
       </jadtool>
 </target>

<target name="copyBBSimulator" if="do.rapc">
    <copy todir="${platform.home}/simulator" verbose="true">
             <fileset dir="${dist.dir}/bbant">
                <include name="**/${name}*"/>
            </fileset>

    </copy>

</target>

<target name="bb-run" depends="copyBBSimulator" if="do.rapc">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
</target>

<target name="bb-debug" depends="copyBBSimulator" if="do.rapc">
    <delete file="${preprocessed.dir}/.timestamp"/>
    <parallel>
          <java jar="${platform.home}/bin/JDWP.jar" fork="true" dir="${platform.home}/bin">
            <jvmarg value="-Xmx128M"/>
          </java>

          <sequential>
              <sleep seconds="5"/>
              <antcall target="nbdebug"/>
         </sequential>
   </parallel>
</target>

2. build.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project [
       <!ENTITY bb-common SYSTEM "bb-build.xml">
]>
<project name="helloLWUIT" default="jar" basedir=".">
    <description>Builds, tests, and runs the project .</description>
    <import file="nbproject/build-impl.xml"/>
    <property name="app.title" value="helloLWUIT" />
    <property name="app.version" value="1" />
    <property name="app.icon"   value="/icon.png" />
    <property name="app.vendor" value="foo" />

     &bb-common;
</project>



3. source code project: helloLWUIT:
  Here
  Environment requirements: please refer Shai's blog for blackberry to set up the project environment


Next Steps:
  1. Figure out how to debug blackberry application on the actual device inside NetBeans;
  2. Make sure how to read attribute in the jad file in the BlackBerry CLDC application;
  3. How to easier copy the customized attribute in the midlet jade file to the BlackBerry CLDC jad file.

References:
  1. http://www.xenglobaltech.com/blackberry/index.php?option=com_content&view=article&id=45&Itemid=40
  2. http://supportforums.blackberry.com/t5/Java-Development/Building-Blackberry-apps-with-Netbeans/td-p/431612


7 comments:

  1. Thanks for the information.
    It's working just as you said.
    Great work Steve, this answers many questions related to this on internet.
    People will definitely remember you for long time.

    ReplyDelete
  2. Hi Steve,
    Thanks for all the help and support you extended to Blackberry Lwuit developers like me. i have followed all the steps as mentioned in your posts and everything seems to work fine, except one issue. after building and signing the cod files, i have uploaded the jad and cod files to remote http server and am trying to download the app to my blackberry via OTA, but when trying to access the link it gives me an error "Error: Invalid manifest or application descriptor. The MIDlet-version number is not a valid version". please help me to resolve this issue.

    ReplyDelete
  3. Hi,Sachin,
    I thought it might due to the version number is set "1", it might need to some value like "1.0" or "1.0.1". I will try this later and give you an update.

    ReplyDelete
  4. Hello,
    Nice blog i like it
    Blackberry devices were first launched with monochrome displays With further amendments and additions to its features, the mobile device now was and is available in colour display.

    blackberry application development

    ReplyDelete
  5. Hello,
    Nice blog i like it
    As more and more smart phone devices and tablet computers are coming in the market, the requirements for developing applications also increasing.

    ReplyDelete
  6. You are the master Steve. I followed your instruction and just copied your predefined build.xml and bb-build.xml. I can debug blackberry code in netbeans. Thanks a lot for sharing your knowledge. It means a lot because we can find what error in our code.

    ReplyDelete
  7. I actually enjoyed reading through this posting.Many thanks.


    Blackberry Application Development

    ReplyDelete