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:

  1. Notice the -dynamiclib flag, this creates a shared library and is required. On linux use -shared instead. 
  2. You have to include Java's jni headers which are always in at least 2 places. 

     

     

    1. $JAVA_HOME/include
    2. $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.
  3. 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
  4. 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. 
  5. 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

Running either sample is straight-forward. However, you need to ensure that native library is on Java's or Scala's library path. The simplest way to do this is to add it to the java.library.path when you start your JVM. 

Java

Scala

 
or 
 

A Note about Deployment

It's not always feasible to set the java.library.path variable (e.g. when using Java Web Start). However, it is not particularly difficult to hack the path used internally by Java to add shared libraries at runtime. This is, however, a topic beyond the scope of this posting.
0 0
原创粉丝点击