This example takes you through the process of using native shared libraries with a Java™ application on AIX® systems.
Procedure
- Create a sample application, Sample.java.
public class Sample
{
public native void printFromNative( );
public static void main( String[] args )
{
Sample sample = new Sample( );
sample.printFromNative( );
}
static
{
String sharedLibrary = "libShared.a(libSample.o)";
try
{
System.loadLibrary( sharedLibrary );
}
catch ( Exception e )
{
System.out.println( "ERROR: Unable to load " + sharedLibrary );
e.printStackTrace( );
}
}
}
- Compile Sample.java.
- Use javah to create a header file for
the native code.
- Create a file called Sample.c.
#include <stdio.h>
#include "Sample.h"
JNIEXPORT void JNICALL Java_Sample_printFromNative( JNIEnv * env, jobject obj )
{
printf( "Printing from native\n" );
}
- Compile Sample.c into libSample.o.
cc_r -bM:SRE -bnoentry -bexpall -Ijava_install_dir/include Sample.c
-o libSample.o -q64
- Create an archive shared library libShared.a.
For
32-bit:
ar -X32 -v -q libShared.a libSample.o
For 64-bit:
ar -X64 -v -q libShared.a libSample.o
- Run the Sample class.
LIBPATH=. java Sample
or
java -Djava.library.path=. Sample
The program will output:
Printing from native
Results
You should now be able to use the same framework to access
native shared libraries from Java applications.