没有合适的资源?快使用搜索试试~ 我知道了~
首页Java programming with JNI
This tutorial deals with the two most common applications of JNI: calling C/C++ code from Java programs, and calling Java code from C/C++ programs. We'll cover both the essentials of the Java Native Interface and some of the more advanced programming challenges that can arise.
资源详情
资源评论
资源推荐

Java programming with JNI
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. About this tutorial....................................................... 2
2. Calling C/C++ code from Java programs .......................... 4
3. Calling Java code from C/C++ programs .......................... 11
4. Advanced topics........................................................ 19
5. Wrap-up and resources ............................................... 25
6. Appendices.............................................................. 28
Java programming with JNI Page 1 of 30

Section 1. About this tutorial
What is this tutorial about?
The Java Native Interface (JNI) is a native programming interface that is part of the Java
Software Development Kit (SDK). JNI lets Java code use code and code libraries written in
other languages, such as C and C++. The Invocation API, which is part of JNI, can be used
to embed a Java virtual machine (JVM) into native applications, thereby allowing
programmers to call Java code from within native code.
This tutorial deals with the two most common applications of JNI: calling C/C++ code from
Java programs, and calling Java code from C/C++ programs. We'll cover both the essentials
of the Java Native Interface and some of the more advanced programming challenges that
can arise.
Should I take this tutorial?
This tutorial will walk you through the steps of using the Java Native Interface. You'll learn
how to call native C/C++ code from within a Java application and how to call Java code from
within a native C/C++ application.
All the examples use Java, C, and C++ code, and are written to be portable to both Windows
and UNIX-based platforms. To follow the examples, you must have some experience
programming in the Java language. In addition, you will also need some experience
programming in C or C++. Strictly speaking, a JNI solution could be broken down between
Java programming tasks and C/C++ programming tasks, with separate programmers doing
each task. However, to fully understand how JNI works in both programming environments,
you'll need to be able to understand both the Java and C/C++ code.
We'll also cover a number of advanced topics, including exception handling and
multithreading with native methods. To get the most out of this part of the tutorial, you should
be familiar with the Java platform's security model and have some experience in
multithreaded application development.
The section on Advanced topics on page 19 is separate from the more basic step-by-step
introduction to JNI. Beginning Java programmers may benefit from taking the first two parts
of the tutorial now and returning to the advanced topics at a later time.
See Resources on page 25 for a listing of tutorials, articles, and other references that expand
upon the material presented here.
Tools and components
To run the examples in this tutorial, you will need the following tools and components:
• A Java compiler: javac.exe ships with the SDK.
• A Java virtual machine (JVM): java.exe ships with the SDK.
ibm.com/developerWorks
Presented by developerWorks, your source for great tutorials
Page 2 of 30 Java programming with JNI

• A native method C file generator: javah.exe ships with the SDK.
• Library files and native header files that define JNI. The jni.h C header file, jvm.lib, and
jvm.dll or jvm.so files all ship with the SDK.
• A C and C++ compiler that can create a shared library. The two most common C
compilers are Visual C++ for Windows and cc for UNIX-based systems.
Although you may use any development environment you like, the examples we'll work with
in this tutorial were written using the standard tools and components that ship with the SDK.
See Resources on page 25 to download the SDK, complete source files, and other tools
essential for the completion of this tutorial.
About the author
Scott Stricker is an enterprise application developer working in Business Innovation Services,
part of IBM Global Services. He specializes in object-oriented technologies, particularly in
Java and C++ programming.
Scott has a Bachelor of Science degree in Computer Science from the University of
Cincinnati. He is a Sun Certified Java 2 Programmer and Developer. Scott may be reached
at sstricke@us.ibm.com.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java programming with JNI Page 3 of 30

Section 2. Calling C/C++ code from Java programs
Overview
JNI allows you to use native code when an application cannot be written entirely in the Java
language. The following are typical situations where you might decide to use native code:
• You want to implement time-critical code in a lower-level, faster programming language.
• You have legacy code or code libraries that you want to access from Java programs.
• You need platform-dependent features not supported in the standard Java class library.
Six steps to call C/C++ from Java code
The process of calling C or C ++ from Java programs consists of six steps. We'll go over
each step in depth in the panels that follow, but let's start with a quick look at each one.
1. Write the Java code. We'll start by writing Java classes to perform three tasks: declare
the native method we'll be calling; load the shared library containing the native code; and
call the native method.
2. Compile the Java code. We must successfully compile the Java class or classes to
bytecode before we can use them.
3. Create the C/C++ header file. The C/C++ header file will declare the native function
signature that we want to call. This header will then be used with the C/C++ function
implementation (see Step 4) to create the shared library (see Step 5).
4. Write the C/C++ code. This step consists of implementing the function in a C or C++
source code file. The C/C++ source file must include the header file we created in Step 3.
5. Create the shared library file. We'll create a shared library file from the C source code
file we created in Step 4.
6. Run the Java program. We'll run the code and see if it works. We'll also go over some
tips for dealing with the more commonly occurring errors.
Step 1: Write the Java code
We'll start by writing the Java source code file, which will declare the native method (or
methods), load the shared library containing the native code, and actually call the native
method.
Here's our example Java source code file, called Sample1.java:
ibm.com/developerWorks
Presented by developerWorks, your source for great tutorials
Page 4 of 30 Java programming with JNI

1. public class Sample1
2. {
3. public native int intMethod(int n);
4. public native boolean booleanMethod(boolean bool);
5. public native String stringMethod(String text);
6. public native int intArrayMethod(int[] intArray);
7.
8. public static void main(String[] args)
9. {
10. System.loadLibrary("Sample1");
11. Sample1 sample = new Sample1();
12. int square = sample.intMethod(5);
13. boolean bool = sample.booleanMethod(true);
14. String text = sample.stringMethod("JAVA");
15. int sum = sample.intArrayMethod(
16. new int[]{1,1,2,3,5,8,13} );
17.
18. System.out.println("intMethod: " + square);
19. System.out.println("booleanMethod: " + bool);
20. System.out.println("stringMethod: " + text);
21. System.out.println("intArrayMethod: " + sum);
22. }
23. }
What's happening in this code?
First of all, note the use of the native keyword, which can be used only with methods. The
native keyword tells the Java compiler that a method is implemented in native code outside
of the Java class in which it is being declared. Native methods can only be declared in Java
classes, not implemented, so a native method cannot have a body.
Now, let's look at the code line by line:
• In lines 3 through 6 we declare four native methods.
• On line 10 we load the shared library file containing the implementation for these native
methods. (We'll create the shared library file when we come to Step 5.)
• Finally, in lines 12 through 15 we call the native methods. Note that this operation is no
different from the operation of calling non-native Java methods.
Step 2: Compile the Java code
Next, we need to compile the Java code down to bytecode. One way to do this is to use the
Java compiler, javac, which comes with the SDK. The command we use to compile our
Java code to bytecode is:
javac Sample1.java
Step 3: Create the C/C++ header file
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Java programming with JNI Page 5 of 30
剩余29页未读,继续阅读














shargue2000
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
最新资源
- ARM Cortex-A(armV7)编程手册V4.0.pdf
- ABB机器人保养总结解析.ppt
- 【超详细图解】菜鸡如何理解双向链表的python代码实现
- 常用网络命令的使用 ipconfig ping ARP FTP Netstat Route Tftp Tracert Telnet nslookup
- 基于单片机控制的DC-DC变换电路
- RS-232接口电路的ESD保护.pdf
- linux下用time(NULL)函数和localtime()获取当前时间的方法
- Openstack用户使用手册.docx
- KUKA KR 30 hA,KR 60 hA机器人产品手册.pdf
- Java programming with JNI
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论1