JNI/Java Native Interface HelloWorld on RHEL7/Java1.8

5 篇文章 0 订阅


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$ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值