Skip Headers
Oracle® Java Micro Edition Embedded Client Developer's Guide
Release 1.1
E20632-02
  Go To Table Of Contents
Contents

Previous
Previous
 
Next
Next
 

2 Compiling Your Application

This chapter describes compiling an application from the command line or integrated development environment (IDE).

This chapter includes these topics:

2.1 Compiling from the Command Line

You need to compile your application on the development workstation (Linux) to create class files that can then be deployed to the target device. Simple shell scripts and command lines are presented in this section to illustrate simple compilation and compiling for debugging or profiling.

2.1.1 Prerequisites

Before compiling, JDK 6.0 must be installed on your development workstation. If you do not yet have it, follow the instructions in Section 2.1.1.1, "Installing the JDK."

2.1.1.1 Installing the JDK

Follow these steps to install the JDK.

  1. Download JDK1.6, update 27 from http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u27-download-440405.html.

  2. Open a Linux terminal and cd into the directory where JDK1.6 has been downloaded. After installation, the environment variable JDK_INSTALL_DIR will be set to this directory.

    Set file permissions to enable execution with the following command:

    sudo chmod a+x jdk-6u27-linux-i586.bin

  3. Run the installer with the following command:

    ./jdk-6u27-linux-i586.bin

2.1.2 Bytecode Requirements for CDC Applications

An Oracle Java ME Embedded Client program must comply with the requirements of the Connected Device Configuration (CDC), version 1.1.2. The correct bytecode will be generated for CDC provided that you specify JDK 1.4 at the command line with both the -source and the -target options. Refer to Section 2.1.3, "Compiling an Application" for an example.

2.1.3 Compiling an Application

  1. On the workstation, cd into the base directory where the source files are located.

  2. Compile with the command

    javac -source 1.4 -target 1.4 helloworld/HelloWorld.java

    The class file HelloWorld.class will be created in the helloworld directory


    Note:

    The value 1.4 must be assigned to the options -source and -target to ensure that the compiled byte codes are compatible with CDC.


2.1.4 Setting Environment Variables

You should make sure that these environment variables are set correctly before running a compiled program on either your development workstation or on the target device.


Note:

This section uses the bash shell. Make adjustments if you use a different shell.


2.1.4.1 CVM_HOME

You need to set the environment variable CVM_HOME to:

InstallDir/Oracle_JavaME_Embedded_Client/binaries

Where InstallDir is the directory on your Linux workstation where you installed the Oracle Java ME Embedded Client.

2.1.4.2 PATH

Your PATH environment variable should be set to include the location of the javac command.

  1. Edit either of the files ~/.bashrc or ~/.bash_profile,

  2. Append the following lines (for example):

    export JAVA_HOME=/home/myname/tools/jdk1.6.0_27

    export PATH=/home/myname/tools/jdk1.6.0_27/bin:$PATH

  3. Log out of your Linux account and log back in.

  4. Use the following command to verify that javac can be found:

    which java


Note:

You can avoid setting the PATH on the target device by entering the relative path or full path to the cvm executable on the command line.


2.1.5 Running an Application on the Target Device

  1. Make sure the class file is accessible by the target host.

    This is typically accomplished by mounting the base directory (where the source and class files are located) as an nfs file system on the device. Or, on devices with less capability, the class file can be copied to a suitable directory on the device with a command such as ftp or scp.

  2. From the workstation, open a terminal window that connects to the target device using a protocol such as ssh or telnet.

  3. Change directory to that containing the cvm executable with the command

    cd cvm_install_dir/bin

    where cvm_install_dir is the location where Oracle Java ME Embedded Client was installed on the device.

  4. Run the program on the device with the command

    ./cvm -cp /home/myname/working/HelloWorld  helloworld.HelloWorld
    

    assuming the first argument is the working directory on the device containing the class file.

  5. Check the terminal output to validate the result.

2.2 Compiling in an IDE

If you prefer to work within an IDE such as NetBeans or Eclipse, you can compile an Oracle Java ME Embedded Client application by setting the project type to Java application, and specifying options to make the compiler generate bytecodes compatible with Java 1.4. For NetBeans, this is illustrated in the following steps.

  1. Create a new project in NetBeans. Choose the category Java and project type Java Application, as shown:

    Figure 2-1 Create New java Application Project

    Description of Figure 2-1 follows
    Description of "Figure 2-1 Create New java Application Project"

    The newly created project creates a default Main class program.

    Figure 2-2 New Java Project in NetBeans

    Description of Figure 2-2 follows
    Description of "Figure 2-2 New Java Project in NetBeans"

Since this is a CDC program, you must change the compiler options to generate JDK 1.4 bytecodes. Select your project in the Projects page and right-click to get the Project Properties dialog box.

Under Categories, choose the first item, "Sources." Near the bottom of the dialog, pull down the Source/Binary Format menu, and select JDK 1.4.

Figure 2-3 Selecting Java 1.4 Encoding

Description of Figure 2-3 follows
Description of "Figure 2-3 Selecting Java 1.4 Encoding"

2.2.1 Setting bootclasspath in an IDE

It is advisable to set bootclasspath when working in an IDE.