![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Java Development Guide Rational Apex can be used for Java development. Apex is integrated with Sun's JDK (Java Developer's Kit) 1.0 or higher compiler and debugger. Apex also supports Sun's method for invoking native (C) programs.
Primary features for building Java applets and applications include:
- Integration of Java package structure with the Apex concepts of subsystems and imports, allowing specification and enforcement of the overall software architecture
- Initialization of new .java and .html files with skeletal code.
- Compilation of Java views using the Compile > Code command.
- The usual Apex mechanisms to run Java applications or invoke the JDK debugger.
- Compilation of mixed C and Java code, including the automated management of intermediate generated artifacts and shared libraries.
- Apex management of the setting of CLASSPATH in the context of imports so builds are reproducible.
- Generation of a disassembly view of Java class files.
- Generation of an error summary window with navigation to the point of error using an Apex editor.
- Support of the Help > Explain command inside an Apex editor window for previously reported errors.
- Version control using traditional Apex mechanisms.
- The installation process sets Apex files appropriately to point at the JDK release.
The .html files created using the File > New HTML command are initialized properly to invoke an applet by the same name as the html file. This file can be run with no changes by Netscape. New .java files contain the outline for a package specification of the same name as the subsystem. The package's class name matches the name of the file in keeping with the Java convention.
The following sections are included in this document:
- Creating Java Programs
- Compiling/Building Java Programs
- Creating Java Native Method Programs
- Compiling/Building Java Native Method Programs
- Running Java/Java Native Method Applications
- Running Java/Java Native Method Applets
- Maintenance Commands
- Additional Notes
Creating Java ProgramsThe online Quick Start Java tutorial describes these steps in detail.
- 1 . Bring up Apex with the java option:
% apexinit -java
This sets the APEX_JAVA_ENABLED session switch to TRUE and will enable Java-specific Apex features.
- 2 . Create a subsystem using the File > New Subsystem command.
- 3 . In the subsystem created above, create a working view using the File > New View command. In the dialog box, specify the Java model corresponding to your architecture and version of Apex you are running.
The model name has the format:
/rational_dir
/base/c++/model.ss/arch.apex_ver
.java.rel
where apex_release is the full pathname to the current Apex release, arch is your computer architecture, and apex_ver is the current Apex version.
For example, for a Sun running Solaris running Apex version 4.2.0, the corresponding Java model is:
.../base/c++/model.ss/sun4_solaris2.4.2.0.java.rel
- 4 . In the view created above, create a Java file using the File > New > New Java command. Specify the name of the file in the New Java dialog box. The .java extension will be used for all Java source files.
When Apex creates a Java file, it uses the Java prototype files located in $APEX_PROTOTYPE_DIR as a blueprint of text to put in the file. The default Java prototype file creates a file with the specified class name and the correct package statement.
- 5 . Edit the Java file(s) using the Java editor which is the Apex text editor with a few additional commands.
Compiling/Building Java Programs
- 1 . To create executable code, execute the Compile > Code command. This command compiles all the Java files into .class files
- 2 . Any Java error messages are displayed in an error summary window. You can traverse from the error summary window to the error location by double-clicking on the location in the error summary window. In the Java editor window, select the Help > Explain command to show the error message.
Creating Java Native Method ProgramsJava native methods are methods that are implemented elsewhere in C or in some other platform-dependent fashion.
To create a Java native method program:
- 1 . Create a subsystem using the File > New Subsystem command.
- 2 . In the subsystem created above, create a working view using the File > New View command. In the dialog box, specify the Java model corresponding to your architecture and version of Apex you are running.
The model name has the format:
/rational_dir
/base/c++/model.ss/arch.apex_ver
.java.rel
where apex_release is the full pathname to the current Apex release, arch is your computer architecture, and apex_ver is the current Apex version.
For example, for a Sun running Solaris running Apex version 4.2.0, the corresponding Java model is:
.../base/c++/model.ss/sun4_solaris2.4.2.0.java.rel
- 3 . To indicate that the view contains native methods, set the BUILD_COMPONENT switch to java_cpp.
- a .
From the project2.ss/java.wrk directory viewer, select the Control > Show > Switches command.The Switches file is displayed in a text editor window.
- b .
Set the BUILD_COMPONENT switch to java_cpp.- c .
Select the Save button.- d .
Select the Close button.- 4 . In the view created above, create a Java file using the File > New Java command. Specify the name of the file in the New Java dialog box. The .java extension will be added to the file name if necessary.
When Apex creates a Java file, it uses the Java prototype files located at $APEX_PROTOTYPE_DIR as a blueprint of text to put in the file. The default Java prototype file creates a file with the specified class name and the correct package statement.
- 5 . Edit the Java file(s) using the Java editor which is the Apex text editor with a few additional commands.
- 6 . Repeat Step 4 and Step 5 to create the file containing the class containing the Java native method. Insert the native method into this class.
- 7 . By default Apex will create a shared library with the same name as the current subsystem (which is also the name of the top-level Java package). This shared library must be loaded by Java when the program runs. The library can be loaded by calling the System.loadLibrary function in the static initialization of a class. Assuming that the library is loaded from the class containing the native method, the code would look like that below:
package project2; public class area { static { System.loadLibrary ("project2"); } static public int get_area (int radius) { native_get_area (radius); } static private native int native_get_area (int radius); }
- 8 . Register the file as a Java method that contains a native method by selecting the Compile > Maintenance > Register Native Methods command.
Compiling/Building Java Native Method Programs
- 1 . To generate the C header and stub source files, select the Compile > Build Management > Generate command.
- 2 . Select the File > New > New C command to create a File Implementation of the native function. Select the Visit It option in the New C dialog box. For the above example, you might create a file called native_get_area.c.
Create the C function using the function declaration found in the generated .h file. For example, if the generated .h file has the name project_area.h and contains the declaration:
extern long project2_area_native_get_area(struct Hproject2_area *,long);The native_get_area.c file should contain:
#include "project2_area.h" long project2_area_native_get_area(struct Hproject2_area *this, long radius)- 3 . Select the Compile > Build Management > Build Everything command on the view. This command does the following in order when applied to a view:
- a .
Generate C from Java for all Java classes that have been registered as containing native methods. This will regenerate the files produced in step 1 above making sure that they reflect the current Java source.- b .
Compile Java.- c .
Compile C, which will create a shared library whose name is based on the subsystem name. This library must be loaded by the Java program. An example of the interfacing Java method that loads this C shared library may be found above in Step 7 of Creating Java Native Method Programs".
Running Java/Java Native Method Applications
- 1 . Select the File > Run command. Enter the main Java method file name in the Execute field.
- a .
When running without the debugger, Apex runs the Java interpreter on the named Java file. It also sets up the LD_LIBRARY_PATH to include the current view and all imported views to simplify access to any shared libraries.- b .
When running with the debugger (select the Debug option) Apex runs the jdb debugger.
Running Java/Java Native Method AppletsTo run a Java applet, you need to display an HTML file that references the applet in a Web browser. The Web browser runs the Java interpreter internally to interpret the applet's compiled code that is in the .class file.
- 1 . Select the File > New > New HTML command. Set the fields in the New HTML dialog box to create the corresponding .html file
Apex creates an HTML file with the HTML code to run the corresponding .class file based on the HTML prototype file. Do not modify this file.
- 2 . Open the .html file in a Web browser.
Maintenance CommandsThe Compile > Maintenance > Clean command removes all compilation artifacts.
The Compile > Show > Disassemble command disassembles the Java .class files to see the byte codes generated for the Java machine. This command can be run on .java or .class files.
Commands that Support Java and HTML
- File > New View
- Java model- File > New > New Java
- File > New > New HTML
- Compile > Code
- Compile > Build Management > Generate
- Compile > Maintenance > Register Native Methods
- Compile > Build Management > Build Everything
- Help > Explain
- File > Run
- Compile > Maintenance > Clean
- Compile > Show > Disassemble
Additional NotesArchitectural Control
The top level Java packages are put into subsystems. Visibility between top level packages is accomplished by importing.
C/Java Support
C and Java can be in the same view.
Sienna is generated by default to provide traversal to header files that come with Java.
C interfacing with Java code requires the C code to be in a shared library. The CREATE_SHARED_LIBRARY switch needs to be set to True (This is the default in the Java model). The shared library name is the name of the subsystem/package.
Java Switches
- JAVA_HOME - The pathname to the java release.
- JAVAC_OPTIONS - Options that are passed to the java compiler.
- JAVA_OPTIONS - Options that are passed to the java interpreter.
Rational Software Corporation http://www.rational.com support@rational.com techpubs@rational.com Copyright © 1993-2002, Rational Software Corporation. All rights reserved. |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |