当前位置: 技术问答>java相关
两个Java文件在一个JVM中,可否访问同一个动态链接库?
来源: 互联网 发布时间:2017-03-13
本文导语: 两个Java文件在一个JVM中,可否访问同一个动态链接库 | 给你写了个例子: 这个是a.java class a { static { try { System.loadLibrary("a"); ...
两个Java文件在一个JVM中,可否访问同一个动态链接库
|
给你写了个例子:
这个是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.java
class b
{
native public int ss(int a);
}
这个是a.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* 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
/* 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
/* 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.4include -ID:jdk1.4includewin32 -LD a.cpp -Fea.dll
运行java a
就可以看到效果了。
这个是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.java
class b
{
native public int ss(int a);
}
这个是a.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* 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
/* 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
/* 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.4include -ID:jdk1.4includewin32 -LD a.cpp -Fea.dll
运行java a
就可以看到效果了。