如何解决当未从 java.exe 目录加载本机库时,JNI 抛出 StackOverflowExcpetion
我用 Java 为 Eric Conway 的 Game of Life 编写了一个 UI,并用 C# 编写了游戏背后的逻辑。使用 JNI,我让它“正常”工作。只要包含游戏逻辑的 DLL 和从 javac -h 生成的 .h 文件编写的 (CPP) DLL 都在 java.exe 目录中,一切正常,程序启动,一切似乎都很完美。
但是,我不想让用户将我的 DLL 复制到他们的 java 目录中(对我来说似乎很傻),只是为了运行程序。目前在我的静态构造函数中,我有 System.load("Bridge");
(我的 C++ DLL 将 java 连接到 C#),只要 Bridge dll 和游戏逻辑 dll 都在 java 目录中,它就可以正常工作。
奇怪的行为:如果我将 System.load
更改为 System.loadLibrary(filePathToDLL);
,程序 出现 以正确加载它(使用 -verbose:jni 选项看到这一点),但程序立即在调用第一个本机 dll 函数时抛出 StackOverflowException(不是在开始执行时,第一个 dll 调用在 main 中稍后出现)。由于这不起作用,我尝试了这个黑客:
try
{
String temp = new File(GOLGUI.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath();
temp = temp.substring(0,temp.lastIndexOf("\\"));
System.load(temp + "\\Bridge.dll");
System.load(temp + "\\GOL.dll");
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
(我在有和没有“GOL”行的情况下都这样做了,(GOL是游戏逻辑dll,但它应该由Bridge加载,所以我认为没有必要开始,但我正在测试一切我想到了))。 在这种情况下,行为相同,抛出 StackOverflowException。
第三次尝试:我设置了一个简单的批处理文件。
java -jar -Xss8m -verbose:jni -Djava.library.path=%~dp0 Conway.jar
@echo off
pause
假设,-Djava.library.path 等应该让它检查与代码中引用的 Bridge dll 的批处理文件相同的目录(此时我恢复到旧的 System.load("Bridge");
)。到目前为止没有任何效果,有什么想法吗?我不知道为什么当 dll 位于 java.exe 目录中时它可以正常工作,但在其他任何情况下都不能正常工作,尤其是当它似乎仍在加载 dll 时。
顺便说一句,这个JNI业务一直在抛出StackOverflowException,我是认真的。在此期间我没有收到“UnsatisfiedLinkError”
有相当数量的代码,不确定所有需要什么,但这是我认为可能需要的主要内容:
Java:
static
{
System.loadLibrary("Bridge");
}
private static native boolean[] GetSingle();
private static native void Handshake(int width,int height);
private static native void SetValue(int pos,boolean value);
private static native void Step();
C++(桥 DLL):
//automatically generated .h file from javac -h (not including the bajillion #defines)
/*
* Class: ui_GOLGUI
* Method: GetSingle
* Signature: ()[Z
*/
JNIEXPORT jbooleanArray JNICALL Java_ui_GOLGUI_GetSingle
(JNIEnv *,jclass);
/*
* Class: ui_GOLGUI
* Method: Handshake
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_ui_GOLGUI_Handshake
(JNIEnv *,jclass,jint,jint);
/*
* Class: ui_GOLGUI
* Method: SetValue
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL Java_ui_GOLGUI_SetValue
(JNIEnv *,jboolean);
/*
* Class: ui_GOLGUI
* Method: Step
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_ui_GOLGUI_Step
(JNIEnv *,jclass);
//cpp file that contains the handles to the C# functions,and the implementations of the above declarations
#include "pch.h"
#include "Bridge.h"
#include "ui_GOLGUI.h"
//using System::Text::Encoding;
array<bool>^ GetSingle()
{
return GOL::GOL::GetSingle();
}
void Handshake(int width,int height)
{
GOL::GOL::Handshake(width,height);
}
void SetValue(int pos,bool value)
{
GOL::GOL::SetValue(pos,value);
}
void Step()
{
GOL::GOL::Step();
}
JNIEXPORT jbooleanArray JNICALL Java_ui_GOLGUI_GetSingle(JNIEnv* env,jclass c)
{
array<bool>^ arr = GetSingle();
jboolean* buff = new jboolean[arr->Length];
for (int i = 0; i < arr->Length; i++)
{
buff[i] = arr[i];
}
jbooleanArray jba = env->NewBooleanArray(arr->Length);
env->SetBooleanArrayRegion(jba,arr->Length,buff);
//env->DeleteLocalRef(jba);
//env->ReleaseBooleanArrayElements()
delete[] buff;
return jba;
}
JNIEXPORT void JNICALL Java_ui_GOLGUI_Handshake(JNIEnv* env,jclass c,jint width,jint height)
{
Handshake(width,height);
}
JNIEXPORT void JNICALL Java_ui_GOLGUI_SetValue(JNIEnv* env,jint val,jboolean isWhite)
{
SetValue(val,isWhite);
}
JNIEXPORT void JNICALL Java_ui_GOLGUI_Step(JNIEnv* env,jclass c)
{
Step();
}
C#(GOL DLL):
//literally all of my C# code,contents of the function probably don't matter too much for issue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GOL
{
public static class GOL
{
private static bool[,] grid;
private static bool[,] buffer;
private static int width;
private static int height;
public static void Handshake(int width,int height)
{
GOL.width = width;
GOL.height = height;
grid = new bool[width,height];
buffer = new bool[width,height];
}
public static void Step()
{
//i horizontal,<- negative i
//j vertical,^- positive j
int CountLiveNeighbors(int i,int j)
{
int sum = 0;
if (i > 0) //left check
{
sum += grid[i - 1,j] ? 1 : 0;
}
if (i < width - 1) //right check
{
sum += grid[i + 1,j] ? 1 : 0;
}
if (j > 0) //down check
{
sum += grid[i,j - 1] ? 1 : 0;
}
if (j < height - 1) //up check
{
sum += grid[i,j + 1] ? 1 : 0;
}
if (i > 0 && j > 0) //bottom left check
{
sum += grid[i - 1,j - 1] ? 1 : 0;
}
if (i < width - 1 && j > 0) //bottom right check
{
sum += grid[i + 1,j - 1] ? 1 : 0;
}
if (i > 0 && j < height - 1) //top left check
{
sum += grid[i - 1,j + 1] ? 1 : 0;
}
if (i < width - 1 && j < height - 1) //top right check
{
sum += grid[i + 1,j + 1] ? 1 : 0;
}
return sum;
}
//calculate and put into buffer.
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
int temp = CountLiveNeighbors(i,j);
if (grid[i,j])
{
//this is a white cell.
if (temp > 1 && temp < 4)
{
buffer[i,j] = true;
}
else
{
buffer[i,j] = false;
}
}
else if (temp == 3)
{
buffer[i,j] = true;
}
else
{
buffer[i,j] = false;
}
}
}
//copy buffer into real grid.
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
grid[i,j] = buffer[i,j];
}
}
}
public static void SetValue(int pos,bool value)
{
int x = pos % width;
int y = (pos - x) / height;
grid[x,y] = value;
}
public static bool[] GetSingle()
{
bool[] result = new bool[width * height];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
result[i + (j * height)] = grid[i,j];
}
}
return result;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。