博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Simple example of using the Java Native Interface
阅读量:6814 次
发布时间:2019-06-26

本文共 5897 字,大约阅读时间需要 19 分钟。

http://www.java-tips.org/other-api-tips/jni/simple-example-of-using-the-java-native-interface.html

————————————————————————————————————————————————————————————

This tip shows you how to program a simple example of using the Java Native Interface. We will write a that calls a C function to print "Hello World!".

The following steps are needed to develop this example:

  • Creating a class (HelloWorld.java) that declares the native method.
  • Using javac to compile the HelloWorld source file.
  • Using javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation.
  • Writing the C implementation (HelloWorld.c) of the native method.
  • Compiling the C implementation into a native library, creating Hello-World.dll or libHello-World.so.
  • Running the HelloWorld program.
  1. Creating a class (HelloWorld.java) that declares the native method

We begin by writing the following program in the Java programming language. The program defines a class named HelloWorld that contains a native method, print.

 class HelloWorld {
     static {
         .loadLibrary("HelloWorld");
     }
     private native void print();
     public static void main(String[] args) {
         new HelloWorld().print();
     }
 }

The first part of the class definition is a static initializer that loads the native library containing the implementation of the print native method. This is followed by the declaration of the print native method and a main method that instantiates the Hello-World class and invokes the print native method for this instance.

Using javac to compile the HelloWorld source file

After you have defined the HelloWorld class, save the source code in a file called HelloWorld.java. Then compile the source file using the javac compiler as follows:

javac HelloWorld.java

This command will generate a HelloWorld.class file in the current directory.

Using javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation

Next we will use the javah tool to generate a JNI-style header file that is useful when implementing the native method in C. You can run javah on the Hello-World class as follows:

javah -jni HelloWorld

The command shown above generates a file named HelloWorld.h.

Writing the C implementation (HelloWorld.c) of the native method

The JNI-style header file generated by javah helps you to write C or C++ implementations for the native method. The function that you write must follow the -prototype specified in the generated header file. You can implement the Hello-World.print method in a C file HelloWorld.c as follows:

 #include <jni.h>
 #include <stdio.h>
 #include "HelloWorld.h"
 
 JNIEXPORT void JNICALL 
 Java_HelloWorld_print(JNIEnv *env, jobject obj)
 {
     printf("Hello World!\n");
     return;
 }
Compiling the C implementation into a native library, creating Hello-World.dll or libHello-World.so

Now that all the necessary C code is written, you need to compile Hello-World.c and build this native library.

Different operating systems support different ways to build native libraries. On Solaris or Linux, the following command builds a shared library called libHello-World.so:

cc -G -I/java/include -I/java/include/solaris       HelloWorld.c -o libHelloWorld.so

The -G option instructs the to generate a shared library instead of a regular Solaris executable file. Because of the limitation of page width in this book, we break the command line into two lines. You need to type the command in a single line, or place the command in a script file. On Win32, the following command builds a dynamic link library (DLL) HelloWorld.dll using the Microsoft Visual C++ compiler:

cl -Ic:\java\include -Ic:\java\include\win32       -MD -LD HelloWorld.c -FeHelloWorld.dll

The -MD option ensures that HelloWorld.dll is linked with the Win32 multithreaded C library. The -LD option instructs the C compiler to generate a DLL instead of a regular Win32 executable. Of , on both Solaris/Linux and Win32 you need to put in the include paths that reflect the setup on your own machine.

Running the HelloWorld program

Because the HelloWorld class contains its own main method, you can now run the program on Solaris or Win32 as follows:

java HelloWorld

You should see the following output:

Hello World!

It is important to set your native library path correctly for your program to run. The native library path is a list of directories that the Java virtual machine searches when loading native libraries. If you do not have a native library path set up correctly, then you see an error similar to the following:

java.lang.UnsatisfiedLinkError: no HelloWorld in library path          at java.lang.Runtime.loadLibrary(Runtime.java)          at java.lang.System.loadLibrary(System.java)          at HelloWorld.main(HelloWorld.java)

Make sure that the native library resides in one of the directories in the native library path. If you are running on a Solaris system, the LD_LIBRARY_PATH environment variable is used to define the native library path. Make sure that it includes the name of the directory that contains the libHelloWorld.so file. If the libHelloWorld.so file is in the current directory, you can issue the following two commands in the standard shell (sh) or KornShell (ksh) to set up the LD_LIBRARY_PATH environment variable properly:

LD_LIBRARY_PATH=.  export LD_LIBRARY_PATH

If you are running on a Windows 95 or Windows NT machine, make sure that HelloWorld.dll is in the current directory, or in a directory that is listed in the PATH environment variable.

In Java 2 SDK 1.2 release, you can also specify the native library path on the java command line as a system property as follows:

java -Djava.library.path=. HelloWorld

The "-D" command-line option sets a Java platform system property. Setting the java.library.path property to "." instructs the Java virtual machine to search for native libraries in the current directory.

 

转载地址:http://abbzl.baihongyu.com/

你可能感兴趣的文章
网络提速(最短路)
查看>>
Spring整合MongoDB实现多个or的范围查询
查看>>
python安装包模块
查看>>
swap内存交换空间构建
查看>>
无标题文章正则表达式
查看>>
存储因管理员策略问题显示脱机解决方法
查看>>
Android Intent Action 大全
查看>>
4412开发板支持GPS高强度信号
查看>>
微信小程序开发-概述
查看>>
SSM(Spring,SpringMVC,MyBatis)用户登录
查看>>
vc代码获取文件版本信息
查看>>
mysql连接小错误一例
查看>>
奇怪的“考生”:中美高考,我都考一考!
查看>>
IBM P系列小型机故障的基本定位
查看>>
The connection cannot proceed because authentication is not enabled
查看>>
7天 搞定 ASP.NET MVC - 第3天
查看>>
云桌面无法识别ica文件
查看>>
分区 fdisk
查看>>
docker registry v2 nginx 安全访问控制
查看>>
Linux中查看各文件夹大小命令du -h --max-depth=1
查看>>