英文:
JVM exits without any error messages when loading compiled DLL
问题
我正在尝试从Java中调用一个C函数。
在加载库(在Test.java中)时,会随机发生两件事:
- 打印“Load Lib”,然后JVM只是退出而没有任何错误
- 打印“Load Lib”,然后JVM在循环中卡住
奇怪的是,“有时候”还会打印“Lib loaded”。这意味着库已加载...
我的问题是如何解决这个问题?真正的问题是我不知道我做错了什么。
DLL编译步骤:
- gcc -fpic -I“C:\Program Files\Java\jdk-15\include” -I“C:\Program Files\Java\jdk-15\include\win32” -c BindLib.c BindLib.h
- gcc -fpic -s -shared -o BindLib.dll BindLib.o
系统信息:
- Windows 10 64位,版本1909
- Java 15
主文件:
package degubi;
public final class Main {
public static void main(String[] args) {
Test.enable();
}
}
库文件:
package degubi;
public class Test {
static {
System.out.println("Load lib");
System.loadLibrary("BindLib");
System.out.println("Lib loaded");
}
public static native void enable();
}
源文件:
#include "windows.h"
#include "BindLib.h"
JNIEXPORT void JNICALL Java_degubi_Test_enable(JNIEnv* env, jclass clazz) {
}
头文件:
#define __int64 long long
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class degubi_Test */
#ifndef _Included_degubi_Test
#define _Included_degubi_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: degubi_Test
* Method: enable
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_degubi_Test_enable(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
英文:
I'm trying to call a c function from Java.
When loading the library (in Test.java) 2 things happen randomly:
- "Load Lib" gets printed, and the jvm just exits without any errors
- "Load Lib" gets printed, and the jvm gets stuck in a loop
The weird thing is that 'sometimes' "Lib loaded" gets printed too. Which means the library got loaded...
My question is that how can I fix this? The real problem is that I don't know what I'm doing wrong.
Dll compilation steps:
- gcc -fpic -I "C:\Program Files\Java\jdk-15\include" -I "C:\Program Files\Java\jdk-15\include\win32" -c BindLib.c BindLib.h
- gcc -fpic -s -shared -o BindLib.dll BindLib.o
System info:
- Windows 10 64 bit, version 1909
- Java 15
Main file:
package degubi;
public final class Main {
public static void main(String[] args) {
Test.enable();
}
}
Library file:
package degubi;
public class Test {
static {
System.out.println("Load lib");
System.loadLibrary("BindLib");
System.out.println("Lib loaded");
}
public static native void enable();
}
Source file:
#include "windows.h"
#include "BindLib.h"
JNIEXPORT void JNICALL Java_degubi_Test_enable(JNIEnv* env, jclass clazz) {
}
Header file:
#define __int64 long long
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class degubi_Test */
#ifndef _Included_degubi_Test
#define _Included_degubi_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: degubi_Test
* Method: enable
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_degubi_Test_enable(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
答案1
得分: 0
请提供代码部分。
英文:
Which shell and C compiler were you using?
The problem might come from the incompatible between C compiler & the shell environment.
For example, if you use Cygwin compiler to compile the DLL, then execute the binary on Windows CMD, the program might not work (dependencies on cygwin.dll).
答案2
得分: 0
我最终在 Visual Studio 中创建了一个项目,并从那里构建... 一切都运行得很完美。仍然不知道是什么导致了这个问题。
英文:
I ended up creating a project in Visual Studio and building it from there... worked perfectly. Still don't know what caused the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论