当未从 java.exe 目录加载本机库时,JNI 抛出 StackOverflowExcpetion

如何解决当未从 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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-