scala和java通过JNI调用C++程序例子
来源:互联网 发布:aix删除网络配置 编辑:程序博客网 时间:2024/05/22 13:28
转载:http://hohonuuli.blogspot.com/2013/08/a-simple-java-native-interface-jni.html 主要是国内访问不了这个网站
First off, let me be perfectly honest, if you have any choice besides using JNI, do that other thing. Dealing with native libraries has been my number one source of pain when supporting Java applications. Now with that warning out of the way, sometimes you just can't avoid using JNI. For those who must go there, I hope this example helps and I salute your bravery and fortitude.
The Java and Scala examples do exactly the same thing. However, the byte code produced is slightly different since Scala doesn't have static methods.
Step 1 – Create a Java or Scala class
Write a Java or Scala class that will interface to the native library:
Java
In Java, methods that will call the native code will use the native keyword in their method signature and have no method body. Create a file named Sample1.java and add the code below:
The above code can be compiled using:
Compiling Sample1.java will produce a single class named, Sample1.class
Scala
In Scala, methods that call native code will use the native annotation and, like Java, have no method signature. Create a file named Sample1.scala and add the code below.
Sample1.scala can be compiled using:
When you compile Sample1.scala you will get three classes: Sample1.class, Sample1$.class, and Sample1$delayedInit$body.class.
Step 2 – Generate a header file with javah
The next step is to have the JVM produce a header file that defines the function prototypes needed for the native implementation.
Java
Creating the native header is really simple for Java. You just need the compiled class, Simple1.class, created in step1. Use the javah program like so:
Notice that you do not use the .class extension. javah will produce a file named Sample1.h.
Scala
Creating a native header from Scala code is slightly more involved in that you may need the scala libraries on the javahclasspath. In our example, since we are using Scala's App class we definitly need the scala libraries. In this example, we only need scala-library.jar and scala-reflect.jar. I'm working on a Mac and installed Scala via homebrew and my classpath below reflects that. Your path will likely be different depending on your platform.
Output from javah will be Sample1.h
The Sample1.h file produced here using Scala looks exactly the same as the file produced using the Java class. Just for grins, this is the output of javah:
Don't you just love C/C++?
Step 3 – Create the native implementation
For both our Scala and Java examples, the native implementation is exactly the same. You can write implementations in either C or C++, although the code will be different. The sample below implements all the methods defined in Sample1.h in C++. Copy and paste the code below into a file named Sample1.cpp:
Now the fun part, getting this to compile on your particular operating system. Since I'm working on a Mac here's the command line blurb I used to compile Sample1.cpp to a shared native library:
A few notes about this:
- Notice the -dynamiclib flag, this creates a shared library and is required. On linux use -shared instead.
- You have to include Java's jni headers which are always in at least 2 places.
- $JAVA_HOME/include
- $JAVA_HOME/include/
. On Macs that's $JAVA_HOME/include/darwin, on Linux it's $JAVA_HOME/include/linux. I have no idea what it is on Windows.
- You may have to set the JAVA_HOME variable on your machine. You can do this on a Mac using: export JAVA_HOME=
/usr/libexec/java_home -v 1.7
- Remember the line in our Java/Scala code that goes: System.loadLibrary(“Sample1″). Although the name of the native library is referenced using “Sample1″ in our code, it's actually looked up using a 'lib' prefix. So the native library is actually named libSample1.dylib on Mac or libSample1.so on Linux.
- If you're stuck developing for Java 6 on a Mac, change the native libraries extension to .jnilib or Java 6 won't find it. Java 7 on Mac requires .dylib extension. (I did try to warn you that JNI will test your strength)
Step 4 – Running the Code
Java
Scala
A Note about Deployment
- scala和java通过JNI调用C++程序例子
- java通过jni调用C程序
- JNI小程序:Java通过JNI调用C程序
- JAVA中通过JNI调用C程序实例教程
- Java通过JNI调用本地C/C++程序
- Java通过JNI调用本地C/C++程序--常用示例
- Android开发 通过JNI实现JAVA与C/C++程序间的调用和回调
- java JNI调用c程序
- Java通过JNI调用C++程序
- Java通过JNI调用C++程序
- Java通过JNI调用C++程序
- Java通过JNI调用C++程序
- Java通过JNI调用C++程序
- Java通过JNI调用CUDA程序
- Java通过JNI调用C++程序
- Java通过JNI调用C++程序
- Java通过JNI调用C++程序
- JAVA通过JNI调用C++程序实践
- 手动修改adb devices的设备ID号
- java ThreadLocal源码详解及用法
- Python--简单的端口扫描器(二)
- AngularJS资源合集[备忘]
- struts2的核心和工作原理
- scala和java通过JNI调用C++程序例子
- ios ios7导航栏返回键设置
- maven使用中遇到的小问题以及解决办法
- chown
- iOS开发之深入探讨runtime机制03-runtime的方法与消息
- TFS Erasure code实现方案
- 存储过程
- MySQL中check语句
- Android动态加载外部jar包及jar包中图片等资源文件