SCOUG Java Programming Class 5
Overview
Class 4 review
Native methods
JDK 1.1
Miscellaneous
Reference notes
Copyright notices
Sample programs
Class 4 Review
general review/questions
additional discussion topics
peers
Native Methods
Native methods allow you to utilize operating system specific capabilities in a non-platform independent mode. You do this by creating a class definition together with an implementation DLL. To create an OS/2 native method class you need to:
- Define a .java file for the native class. This class file should include:
- Public variables as needed for the class and its methods
- Public methods which will be directly invoked by other Java class objects. These methods will normally perform minimal processing and are mainly used as wrappers to invoke the appropriate implementation DLL entry points.
- A static invocation of the implementation DLL.EG: static { System.loadLibrary("myLib"); } This will cause the DLL to be loaded, if necessary, when the Java class itself is first loaded.
- Definition of all of the implementation DLL's entry points as "public native". EG: public native entry1(String s, int x);
- Compile this class using the normal javac compiler to produce the .class file
- Create a C header file for the native class by using javah program. EG javah NativeClass1. This file will contain the includes and external references for your methods.
- Create a C stub file for the native class by again using the javah program, this time with the stubs option. EG javah -stubs NativeClass1. This creates a stub module (.c) for the implementation DLL to provide appropriate linkage between the Java VM interpreter and your DLL.
- Code the implementation DLL. The name is arbitray (and must be referenced in the class static invocation as described above). The entry point names must be coded as the class name, followed by one underscore character, followed by the native method name. For the example used above the entry point name would be: NativeClass1_entry1. When coding the entry points you can refer to the header file for parameter passing details. When working with Java string objects, there are several utility routines provided to convert between native c-language strings and the string objects. Java int variables and boolean variables are implemented as type long. Each entry point receives:
- a structure containing pointers to all of the classes public variables (so that you can access them)
- a pointer (or value) for each parameter named in the method
- Compile and link your implementation DLL. It's a good idea to use a makefile. The include path used for compiling should include: ..\javaos2\include and ..\javaos2\include\os2. You must link the object module resulting from the compile of the stub routine created above into the DLL. You should use the javai.lib library to get access to the various native method utility routines. (The lib path used for linking should include: ..\javaos2\lib.) The DLL should be moved to the directory containing your other class files.
- Other notes:
- The loadLibrary method is subject to the same security restrictions as other operating-system sensitive methods (such as file i/o) and thus is normally not allowed to execute when invoked from a browser environment.
- You can adversely affect Java's garbage collection scheme (in OS/2 1.0.2 JDK) by executing code in your native methods which performs indefinite waits (more than 1 second). Instead they recommend you use the ThreadCheckForSuspend function periodically.
- If your code does floating point operations you should save and restore the floating point control word.
- You should do your best to clean up OS/2 resources allocated by your native methods when appropriate since Java runtime will not normally do so.
JDK 1.1
The recently released JDK 1.1 (not yet available on OS/2 platform) contains a number of enhancements to the standard supplied class library. A few of the significant ones are:
- Security extensions (signature and certificate verification)
- AWT enhancements (especially printing interface, popup menus, integrated scrolling, clipboard, component cursors)
- Archive format (compressed, combines many class and resource files into one) - JAR
- Component model (JavaBeans)
- Listener class capabilities
- Object serialization/deserialization
- Remote Method Invocation (RMI)
- Database connectivity (standardized SQL interface classes, also includes ODBC bridge)
Other
Performance test results
Server-side Java objects (standard webservers, Jeeves)
non-browser,server environments (Castanet)
application development (COJ)
Reference Notes
Copyright Notices
Java and JDK as referenced herein are protected by the following copyright notices:
(c) Copyright Sun Microsystems, Inc., 1992-1997. All rights reserved.
(c) Copyright IBM Corporation, 1996-1997. All rights reserved.
The JDK is based in part on the work of the Independent JPEG group.
The Java(tm) technology is owned and exclusively licensed by Sun Microsystems, Inc. Java(tm) is a trademark of Sun Microsystems, Inc. in the United States and other countries.
The JDK implementation used in this class is OS/2 version 1.0.2 and is used with permission of IBM Corporation. This use is governed by the terms and conditions specified in the beta license agreement provided by IBM Corporation.