给你写了个例子:这个是a.java class a { static { try { System.loadLibrary("a"); } catch(Exception e) { System.out.println(e); } } public static void main(String args[]) { a m = new a(); System.out.println(m.s(123)); b o = new b(); System.out.println(o.ss(567)); } native public int s(int a); }这个是b.javaclass b { native public int ss(int a); }这个是a.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class a */#ifndef _Included_a #define _Included_a #ifdef __cplusplus extern "C" { #endif /* * Class: a * Method: s * Signature: (I)I */ JNIEXPORT jint JNICALL Java_a_s (JNIEnv *, jobject, jint);#ifdef __cplusplus } #endif #endif这个是b.h/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class b */#ifndef _Included_b #define _Included_b #ifdef __cplusplus extern "C" { #endif /* * Class: b * Method: ss * Signature: (I)I */ JNIEXPORT jint JNICALL Java_b_ss (JNIEnv *, jobject, jint);#ifdef __cplusplus } #endif #endif这个是我修改后的 a.h/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class a */#ifndef _Included_a #define _Included_a #ifdef __cplusplus extern "C" { #endif /* * Class: a * Method: s * Signature: (I)I */ JNIEXPORT jint JNICALL Java_a_s (JNIEnv *, jobject, jint);
可是不是先要编译.H文件吗?两个java文件用一个动态库该怎么办?
class a
{
static
{
try
{
System.loadLibrary("a");
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
a m = new a();
System.out.println(m.s(123));
b o = new b();
System.out.println(o.ss(567));
}
native public int s(int a);
}这个是b.javaclass b
{
native public int ss(int a);
}这个是a.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class a */#ifndef _Included_a
#define _Included_a
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: a
* Method: s
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_a_s
(JNIEnv *, jobject, jint);#ifdef __cplusplus
}
#endif
#endif这个是b.h/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class b */#ifndef _Included_b
#define _Included_b
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: b
* Method: ss
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_b_ss
(JNIEnv *, jobject, jint);#ifdef __cplusplus
}
#endif
#endif这个是我修改后的 a.h/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class a */#ifndef _Included_a
#define _Included_a
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: a
* Method: s
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_a_s
(JNIEnv *, jobject, jint);
JNIEXPORT jint JNICALL Java_b_ss
(JNIEnv *, jobject, jint);#ifdef __cplusplus
}
#endif
#endif这个是a.cpp#include "a.h"
JNIEXPORT jint JNICALL Java_a_s
(JNIEnv *, jobject, jint)
{
return 999;
}
JNIEXPORT jint JNICALL Java_b_ss
(JNIEnv *, jobject, jint)
{
return 888;
}用这个命令编译:cl -ID:\jdk1.4\include -ID:\jdk1.4\include\win32 -LD a.cpp -Fea.dll运行java a就可以看到效果了。
可是你说的办法是两个java文件用同一个动态库中的不同函数,
如果我想用同一个动态库中的同一个函数有办法没?我总不至于
把同一个函数就头改一下写两遍吧?太土了。
不过你说的也是一个解决办法,先给一半分吧,如果都解决了再给另外一半。