1 Write a Java class that uses C codes
Copy public class CraftJNI {
static {
System . loadLibrary ( "hello" ); // Load native library hello.dll (windows)
// or libhello.so (Unixes) at runtime.
// This library contains a native method sayHello()
}
// Declare an instance native method sayHello() which receives no parameter
// returns void
private native void sayHello ();
// Test Driver
public static void main ( String [] args) {
new CraftJNI() . sayHello (); // Create an instance and invoke native method
}
}
2 Compile Java program CraftJNI.java
Copy /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class CraftJNI */
#ifndef _Included_CraftJNI
#define _Included_CraftJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: CraftJNI
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_CraftJNI_sayHello
( JNIEnv *, jobject );
#ifdef __cplusplus
}
#endif
#endif
Copy javap -verbose CraftJNI.class
Copy #include <jni.h> // Standard JDK provided header
#include <stdio.h> // Standard C IO header
#include <CraftJNI.h> // Generated header
JNIEXPORT void JNICALL Java_CraftJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("I am in C code \n");
return;
}
Copy gcc -fPIC -I "." -I "$JAVA_HOME/include" -I "$JAVA_HOME/include/linux" \
-shared -o libhello.so CraftJNI.c
Copy > java -Djava.library.path = . CraftJNI
I am in C code
Error 1: class file has wrong version
Copy > javah CraftJNI.class
Error: cannot access CraftJNI
bad class file: ./CraftJNI.class
class file has wrong version 55.0, should be 52.0
Copy > java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-post-Debian-1deb10u2 )
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-post-Debian-1deb10u2, mixed mode, sharing )
> javac -version
javac 11.0.9.1
> javah -version
javah version "1.8.0_252"
> whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
> ls /usr/share/man/man1 | grep java*
java.1.gz
javac.1.gz
javadoc.1.gz
javah.1.gz
javap.1.gz
Copy > java -Djava.library.path = . CraftJNI
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in java.library.path: [.]
at java.base/java.lang.ClassLoader.loadLibrary ( ClassLoader.java:2670 )
at java.base/java.lang.Runtime.loadLibrary0 ( Runtime.java:830 )
at java.base/java.lang.System.loadLibrary ( System.java:1873 )
at CraftJNI. < clini t >( CraftJNI.java:4 )