# 3.

Java Projects

This chapter explains how to create a Java project.

## Overview of Java Projects

The topics covered in this section icnlude:

- `Installing Metrowerks Java`
- `Java Tools & Environment`
- `Where to Learn More About Java`
- `What You See`
- `Working with Java in CodeWarrior`

## Installing Metrowerks Java

Use the CodeWarrior installer application and follow the instructions in the application.

Select one of the Java-related options to install all of the Java tools. We do not recommend that you install the tools manually. The installer ensures that everything is installed in the proper locations.

## Java Tools & Environment

Once the Java tools are installed, the following topics will describe the various Java files and their uses, then tell how to modify their location.

- `Metrowerks Java Tools`
- `Environment Variables`

### Metrowerks Java Tools

The Metrowerks Java Tools are based upon Sun's Java Development Kit 1.1.5, and currently consists of the following command-line tools and files:

Tool | Description
--- | ---
appletviewer | Java applet viewer
`be.zip` | Be-specific class files
`classes.zip` | Java class files
jar | Java archive tool
`java` | Java Interpreter
`javac` | Java Compiler
`javadoc` | Java documentation generator
`javah` | Native method C file generator
javakey | Key Management and Digital Signatures
javap | Java class dissassembler
javaverify | Java class verification application
jre | Java runtime environment
native2ascii | Converts any text file to ASCII
serialver | Class Version Number Generator
`libjava.so` | Java Shared Library
`javanet.so` | Java Network Shared Library
`javaawt.so` | AWT Shared Library
`javammedia`.so | Java Shared Library (for sound)
`JavaCompilerPlugin` | BeIDE plug-in
JavaHeaders | Directory containing the headers necessary to compile native methods

### Environment Variables

The following environment variables identify the locations of the Java related files:

Variable | Description
--- | ---
JAVA_HOME | Location of Java related files. Default setting is: `/boot/apps/Metrowerks/java`
`JAVA_LIBRARY_PATH` | Path to Java libraries. Default setting is: `$JAVA_HOME/lib`
CLASSPATH | Path to Java methods. Default setting is: .`$JAVA_HOME/`lib/classes.zip:`$JAVA_HOME/`lib/be.zip:`$JAVA_HOME/`lib/

If you wish to change where the Java files are located, you must set the `JAVA_HOME`, `JAVA_LIBRARY_PATH`, and `CLASSPATH` environment variables in the file UserSetupEnvironment, located at this path:

`/boot/home/config/boot/UserSetupEnvironment`

For example, if you want to locate the `java` directory inside the
`develop` directory, UserSetupEnvironment might look like this:

    JAVA_HOME=/boot/develop/java

    JAVA_LIBRARY_PATH=$JAVA_HOME/lib

    CLASSPATH=$JAVA_HOME/classes/classes.zip:$JAVA_HOME/classes/
    					be.zip:$JAVA_HOME/classes

## Where to Learn More About Java

This manual does not teach Java syntax, nor does it introduce you to the classes and methods in the Java API-the calls you use to program the Java virtual machine.

You should consult the Java Language Tutorial. This HTML document is available directly from Sun Microsystems at:

`http://java.sun.com/doc.html`

To learn more about the Java API, consult the Java API Documentation, available as part of the CodeWarrior documentation. This document is in HTML format and comes from Sun Microsystems.

## What You See

Once you have Java installed correctly on your machine the Settings window will display four Java related setting panels. These are:

- `Java Project`
- `Java Compiler`
- `Java Disassembler`
- `Java Linker`

### Java Project

Use the Java Project settings panel to specify what kind of Java project you have.

The available project types are Application, Applet, and Library.

When the project type is Application, the Main Class edit field is displayed. Enter the class name that contains the main class in this field.

When the project type is Applet, the URL edit field is displayed, as shown in `Figure 3.1`. The URL can be specified as an absolute path, a path relative to your project folder, or as a remote path on the internet.

#### Figure 3.1 Java Project settings panel for an Applet

### WARNING!

When entering the name of a URL, you must use the correct capitalization and syntax for the URL to be recognized by the CodeWarrior compiler. If you do not enter the URL precisely, the compiler will not find your HTML file and your applet will not launch.

When the project type is Library, no other fields are shown.

### Java Compiler

Use the Java Compiler panel to specify how CodeWarrior compiles your Java source code into Java byte-code.

#### Figure 3.2 Java Compiler settings panel

#### Code Generation

When generating Java source code into Java byte-code, use one of the following options:

- Normal-compiles Java source code into standard Java byte-code (stored in .class files).
- Debugging-compiles Java source code with additional information to allow debugging.
- Optimized-compiles optimized Java source code.

#### Show Warnings

Enable this option to display Java warnings.

### Java Disassembler

Use the Java Disassembler panel to specify how CodeWarrior disassembles your Java byte-code.

#### Figure 3.3 Java Disassembler settings panel

#### C Header Style

Turn on C Header Style to output class declarations using C syntax instead of Java syntax. Turning this option on disables all other options.

#### Disassemble

Turn the Disassemble option on to dissassemble all methods, incuding `protected` and `private` methods, regardless of the Show Private Fields setting.

#### Show Private Fields

Use Show Private Fields to output the contents of private and protected methods and variables as well as public fields. When Show Private Fields is enabled the public, private, and protected methods and variables are all output. When Show Private Fields is disabled, only the public methods and variables are output.

#### Show Line Number Tables

Use Show Line Number Tables to output line numbers and local variable tables to the public fields of the class. When Show Line Number Tables is enabled, line numbers and local variable tables are output. When disabled, only public fields are output.

#### Verbose

Use Verbose to output additional information (in the form of Java comments) about each member of each specified class.

### Java Linker

Currently, there are no option settings for the Java Linker.

#### Figure 3.4 Java Linker settings panel

## Working with Java in CodeWarrior

This section describes how to create, run, and debug any kind of Java project, including applets, applications, and libraries. The topics in this section are:

- `Creating a New Java Project`
- `Running a Java Project`
- `Running an Example Java Application from the IDE`

### Creating a New Java Project

The following procedure describes how to create a new Java project. The project can be either an applet, an application, or a library of Java code.

### TIP:

Included in this patch is a folder called MahJongg Source. This Java applet sample code was used in this tutorial, and it is useful to follow the steps using this source code.

1. Create a new project.

Choose `New Project` from the File menu. CodeWarrior displays the New Project and BeIDE:Save dialog boxes.

Click the BeIDE:Save dialog box to bring it to the front, and navigate to the destination folder of your project. If you want to create a new folder, choose the `New Folder` command from the `File` menu.

After naming the project, click `Save` to continue.

After saving the name of the project, you are ready to work with the new CodeWarrior project window, as shown in `Figure 3.5`.

#### Figure 3.5 New Java project window

2. Add Java support files.

Every Java project must contain the Java support files `
classes.zip`, `be.zip` and `compiler.jar`. These files are located in the directory

`/boot/apps/Metrowerks/`java/lib``

You can add these files to the project by dragging and dropping them into the project window, or by choosing the menu item Add Files from the Project menu.

### NOTE:

At the time of this writing, there is an extra step you need to take when adding compiler.jar to the project. You'll see a note in the info window that compiler.jar couldn't be added to your project because there's no matching record in the Target preferences panel.

To get around this, choose Settings from the Window menu, and click the Target settings panel from the list on the left. Scroll down to the extension .zip, and click this entry. Next, enter the text `jar` into the Extension edit field, and click Add. This allows the compiler to recognize jar files in your project.

Now, add `compiler.jar` to your project.

#### Figure 3.6 Add Java support files to project

The `classes.zip` file is a shared library of standard Java classes your applet can use.

3. Remove the Be library files.

Remove the library files `libroo`t.so, libbe.so, and `libdll.a` from your new project. They are not required when creating Java applications, applets or libraries. Shift-click each file to select the files, and then choose Remove Selected Items from the Project menu.

#### Figure 3.7 Remove Be support files

4. Add Java files to your project.

Either drag and drop the files directly to the project, or choose the menu item `Add Files` from the `Project` menu.

5. Create a Group for your source files.

If you want to separate your libraries from your source files, you can create a seperate group for your source files. Select a source file by clicking it in the project window, and choose New Group from the Project menu.

You can now rename this group by double clicking the default name New Group. Finally, drag all of your source files under your new heading.

#### Figure 3.8 Creating a new group for your source files

### Running a Java Project

You may run either an applet or an application. You cannot run a library. To run a project, choose `Run` from the `Project` menu. At that time, CodeWarrior compiles and links any changed files, and launches your program. Precisely how your program launches depends upon whether it is an applet or an application.

#### Running a Java applet

When developing an applet, you typically have a small HTML file that embeds the applet code. CodeWarrior uses this file to run your program from within the IDE. When you choose `Run` from the `Project` menu, CodeWarrior opens the HTML file specified in the URL edit field of the `Java Project` settings panel.

### NOTE:

You can add as many HTML files as you want to your project. If your applet is complex, you may want several HTML files that test different aspects of your applet. To change the HTML file that CodeWarrior uses, simply specify another HTML file in the Java Project settings panel.

You can also run an applet by opening one of its HTML files with any Java-enabled browser.

You can add as many HTML files as you want to your project. If your applet is complex, you may want several HTML files that test different aspects of your applet. To change the HTML file that CodeWarrior uses, simply specify another HTML file in the Java Project settings panel.

You can also run an applet by opening one of its HTML files with any Java-enabled browser.

Running a Java application

To run a Java application, choose `Run` from the Project menu in the CodeWarrior IDE. The IDE launches AppletViewer, passing it the class name specified in the ```Java Project``` panel.

### Running an Example Java Application from the IDE

In this section we'll open an example project containing a Java application. We want to make sure the project's settings are specified correctly, and then we'll run the project to view it's output.

1. Locate the example project.

Go to the path

`/boot/java/examples/HelloWorldApplication`

and open the project `HelloWorld.proj`

2. Check the project settings.

Choose the menu item `Settings` from the `Window` menu to display the settings dialog box. Select the panel `Java Project`, and make sure that this information is listed.

- The Project Type should be Application.
- The Main Class is `HelloWorld`

Now that you've verified these settings, close the Settings dialog box.

3. Run the application.

Choose the menu item `Run` from the `Project` menu to run the application.

### NOTE:

Before you run your application, make sure that the debugger is disabled. To disable the debugger, make sure the menu item `Enable Debugger` is shown under the `Project` menu.

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: support@metrowerks.com
Copyright © 1998, Metrowerks Corp. All rights reserved.
Last updated: February 15, 1998
* Chris Magnuson * John Roseborough