Now this process isn't 100% ideal. If you are an experienced developer you probably already know you need four key components.
- Rapc compiler
- Signature tool
The first two components works fabulously in Linux. The second (signature tool) needs a kick in the pants to work (shame shame shame!).
And the fourth I just can't get past. There is the barry project which has made impressive progress in the last few months and lists javaloader support as on the roadmap for milestone 4. RIM, if you have any sympathy for us non-windows users show these guys some love and send them some specs for heaven sakes!
This article presents the basics for compiling, signing, and debugging BlackBerry applications with Linux.
First you need a copy of the JDE or the JDE components package. In my last article I covered a method of extracting the install packages distributed by RIM, but if you have access to a Windows box copying the installed JDE works just fine too.
The rapc compiler needs two external commands:
- Java Compiler (javac)
- Preverification Tool (preverify)
Install the Sun JDK to get javac:
$ sudo apt-get install sun-java5-jdk
The preverify command can be obtained by downloading the Sun WTK.
$ chmod +x sun_java_wireless_toolkit-2_5_2-linux.bin $ ./sun_java_wireless_toolkit-2_5_2-linux.bin
For some reason this package complains it can't find Java interpreter. If this happens, the Java 5 JDK is located
/usr/lib/jvm/java-1.5.0-sun/bin on Ubuntu.
Once extracted, unless you plan on doing vanilla J2ME development, you can copy the preverify tool and then delete the WTK.
$ mkdir ~/bin $ cp ~/lib/WTK2.5.2/bin/preverify* ~/bin $ rm -r ~/lib/WTK2.5.2
Don't forget to put the
~/bin directory in the execution path. To have this set automatically each time you login, put this line in your
~/.bash_profile file (create it if necessary).
Compile "Hello, World!"
I'll be using Apache Ant for building because it's the common denominator when it comes to building Java projects (like it or not). Most IDE's have some level of support for running ant scripts.
$ sudo apt-get install ant
Download bb ant tools and put it where ant can find it:
$ unzip bb-ant-tools-0.7-bin.zip $ mkdir -p ~/.ant/lib $ cp bb-ant-tools.jar ~/.ant/lib
Download the hello world project and run
ant in the project directory:
$ wget http://slashdev.ca/assets/download/hello.tar.gz $ tar -xzf hello.tar.gz $ cd hello $ ant
Output from the ant should look something like this:
josh@ubuntu:~/hello$ ant Buildfile: build.xml build: [mkdir] Created dir: /home/josh/hello/build [rapc] Compiling 1 source files to ca_slashdev_hello.cod BUILD SUCCESSFUL Total time: 1 second
To sign your cod files, follow my guide on how to fix the signature tool. Then just run the sign target in the build script.
Running Windows in a virtual machine may seem like a cumbersome process but it can be made relatively painless. VirtualBox is very popular and available for most if not all Linux distros. I'm using Ubuntu but you could just as easily apply most of the steps to other distros.
There is an excellent guide here with instructions on how to get it up and running. But here is the short version:
$ sudo apt-get install virtualbox-ose $ sudo gpasswd -a `whoami` vboxusers $ sudo modprobe vboxdrv
Launch VirtualBox from the System Tools category in the gnome menu. Hint: to make VirtualBox look a little less out of place in a gtk environment:
$ sudo apt-get install polymer qt3-qtconfig
Launch qtconfig and select Polymer from the GUI Style list:
I'll save the boring details of installing Windows in a virtual machine. Here is a list of software that you will need to install in Windows once you have it installed.
Now this bit is most important. We need Windows to appear as just another host on the network so that we can connect to JDWP running in Windows from Eclipse running in Linux. We also want to mount a windows shared folder in Linux to quickly copy files to the simulator directory.
It's not enough to just configure the virtual machine to use Host interface, we need to setup a bridge in Linux too. There are hundreds of guides on the interweb describing how to do this. Google "virtualbox host networking" and you will get tons of great links.
I am really only concerned with having two-way communication between Linux and Windows so I am using an unused network interface on my system to setup the bridge. This interface is not connected to my network and gets statically assigned a private IP. If you don't have a spare interface, or you use wireless networking, see this guide for help.
$ sudo apt-get install bridge-utils uml-utilities
Script to bring up the bridged interface:
#!/bin/bash MYUSER=$USER # create new bridge sudo brctl addbr br0 # bright down the ethernet interface and add it to the bridge sudo ifconfig eth0 0.0.0.0 sudo brctl addif br0 eth0 # set bridge to static private address sudo ifconfig br0 10.0.0.1 netmask 255.255.255.0 # create tap interface sudo tunctl -b -t tap0 -u $MYUSER sudo ifconfig tap0 up sudo brctl addif br0 tap0
Now simply run the script once prior to starting Windows. In this case the interface on the host (Linux) is not connection to the network and has a static IP so I must also set a static IP in the guest (Windows).
In Windows Explorer, locate the directory where you have installed the JDE, right click the simulator directory and click "Sharing and Security". I think this dialog is different depending on the edition and service pack of windows but for me (XP Pro, SP2) I just had to check "Share this folder on the network" and "Allow network users to change my files".
Running the Debugger
Start JDWP in Windows. Back in Linux, mount the Windows shared directory:
$ sudo mkdir /mnt/simulator $ sudo mount -t cifs //10.0.0.2/simulator /mnt/simulator
Use the load-simulator target from the build script in the hello world project to copy the required files over to the mounted simulator directory.
$ ant load-simulator
Run the debugger! For some reason an exception occurs when using
jdb at some point, but simply type
resume<enter> to get past it. When using Eclipse, I don't have this problem.
$ jdb -connect com.sun.jdi.SocketAttach:hostname=10.0.0.2,port=8000
And finally there is one last trick to make using the simulator in a Windows virtual machine a bit more enjoyable: Seamless Mode. Provided you installed the guest additions in Windows you can activate this by selection "Seamless Mode" from the "Machine" menu in the VirtualBox window. This hides the Windows desktop and makes it look like the applications running in Windows are actually running in your Linux desktop.