JNI javah相关

关于路径找不到的问题。

文件在/home/xxxxx/workspace/JNI_Test/org/dvb/test/NativeStuff.class

org.dvb.test.NativeStuff  package名字

In order to be sure, use -classpath option:

javah -classpath /home/xxxxx/workspace/JNI_Test  org.dvb.test.NativeStuff

注意输类的名字  org.dvb.test.NativeStuff

(And make sure the /home/xxxxx/workspace/JNI_Test/org/dvb/test/NativeStuff.class file exists)


http://home.pacifier.com/~mmead/jni/cs510ajp/

One of the best ways to learn a new programming skill is by example. Inkeeping with tradition, we present the canonical programming example thatsimply prints the words "Hello World!" to the display. The twist is thatJava code will invoke a native function to do the actual printing via the Clibrary function, printf. A more detailed explanation of this examplecan be foundhere.

1. Create the Java files. First, create the two Java files as shown below.

HelloWorld.java
class HelloWorld 
{
  public native void displayMessage();
  static 
  {
    System.loadLibrary("HelloWorldImp"); 
  }
}
Main.java
class Main 
{
  public static void main(String[] args) 
  {
    HelloWorld hello = new HelloWorld();
    hello.displayMessage();
  }
}
2. Compile the Java files.
javac HelloWorld.java
javac Main.java
3. Create the header file. Uses the .class file created previouslyto create HelloWorld.h.(Note the -jni argument to javah.)
javah -jni HelloWorld
4. Create the C++ file. This example shows the native code with a .cppextension, but this file could just have easily had a .c extension since thereis nothing "C++" about it.

HelloWorld.cpp
#include <stdio.h>
#include "HelloWorld.h"   // this header file was generated by javah
JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj) 
{
  printf("Hello World!\n");
}
5. Build the shared library from native code. This is where some of the differences show up. Make sure that the search paths are correct and thatthe file extensions are what the compiler expects. Also, the convention inUnix is to prepend lib onto the names of library files and to append .so onto the names of shared library files.
# This works on sirius.cs.pdx.edu, note .C extension
g++ -G -I/pkgs/jdk1.1.1/include -I/pkgs/jdk1.1.1/include/solaris HelloWorld.C -o libHelloWorldImp.so

: This should work under NT. Replace the include paths to match your environment
: and be sure to include Sun's JDK not Microsoft's!
cl -Im:\jdk1.1.5\include -Im:\jdk1.1.5\include\win32 -LD HelloWorld.cpp -FeHelloWorldImp.dll
6. Execute the program. On Unix, you may have to move the shared libraryinto a directory that's in your search path, or add the current directory toyour path. Under Windows NT, the current directory is searched by default.
java Main

Hello World!  <--- this is displayed on the screen

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值