lake@localhost:~/java$ uname -a
Linux localhost.localdomain 4.13.0+ #5 SMP Mon Apr 23 04:09:02 EDT 2018 x86_64 x86_64 x86_64 GNU/Linux
lake@localhost:~/java$ javac -version
javac 1.8.0_65
lake@localhost:~/java$ java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)
lake@localhost:~/java$ mkdir jini
lake@localhost:~/java$ cd jini/
lake@localhost:~/java/jini$ cat HelloWorld.java
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
lake@localhost:~/java/jini$
lake@localhost:~/java/jini$ javah -jini HelloWorld.
HelloWorld.class HelloWorld.java
lake@localhost:~/java/jini$ ls -l
total 8
-rw-r--r-- 1 lake sys 442 Sep 21 02:20 HelloWorld.class
-rw-r--r-- 1 lake sys 191 Sep 21 02:20 HelloWorld.java
lake@localhost:~/java/jini$ javah -jni HelloWorld
lake@localhost:~/java/jini$
lake@localhost:~/java/jini$ ls -l
total 12
-rw-r--r-- 1 lake sys 442 Sep 21 02:20 HelloWorld.class
-rw-r--r-- 1 lake sys 377 Sep 21 02:20 HelloWorld.h
-rw-r--r-- 1 lake sys 191 Sep 21 02:20 HelloWorld.java
lake@localhost:~/java/jini$ cat HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
lake@localhost:~/java/jini$
lake@localhost:~/java/jini$ cat HelloWorld.c
#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}
lake@localhost:~/java/jini$ find /usr/ -name jni.h
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91-2.6.2.3.el7.x86_64/include/jni.h
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64/include/jni.h
lake@localhost:~/java/jini$ gcc -I /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91-2.6.2.3.el7.x86_64/include -I /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64/include/linux -fPIC -shared -o libHelloWorld.so HelloWorld.c
lake@localhost:~/java/jini$ ls
HelloWorld.c HelloWorld.class HelloWorld.h HelloWorld.java libHelloWorld.so
lake@localhost:~/java/jini$
lake@localhost:~/java/jini$ java HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at HelloWorld.<clinit>(HelloWorld.java:8)
lake@localhost:~/java/jini$ export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH
lake@localhost:~/java/jini$ java HelloWorld
Hello World!
lake@localhost:~/java/jini$