我正在开发一个JavaFX应用程序&一个C ++ DLL我的方法我想从JavaFX(java代码)调用。因此,我已将dll放在项目目录中,在运行时获取工作目录,因此创建了dll的路径,将相同的路径值设置为" java.library.path" &安培;然后在静态块中加载dll。加载后,当我尝试访问dll的方法时,每次我得到"不满意LinkError"。
请指导我上述问题。 JavaFX JNI应用程序是否有效?
以下是代码:
package jni;
public class Test{
static {
try {
System.out.println("Current Directory :" + System.getProperty("user.dir"));
String path = System.getProperty("user.dir") + "\\dll";
System.setProperty("java.library.path", path);
System.loadLibrary("Test");
System.out.println("Loaded DLL");
System.out.println("java.library.path : " + System.getProperty("java.library.path"));
} catch (UnsatisfiedLinkError ex) {
System.out.println("\n Link Issue :" + ex.getStackTrace());
} finally {
}
}
public static native void nativeTest();
public static void NativeTestJni() {
try {
System.out.println("\n Called Frm MEthod");
nativeTest();
} catch (UnsatisfiedLinkError ex) {
System.out.println("\n Erase Called from Method");
} finally {
}
}
}
package xyz;
import jni.Test
import javafx.scene.image.Image;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class TestJNI extends Application {
@Override
public void start(Stage stage) throws Exception {
primaryStage.setTitle("My First JavaFX App");
primaryStage.show();
Test.NativeTestJni();
System.out.println("\n called ended frm obj");
}
public static void main(String[] args) {
launch(args);
}
}
我正在为我正在打印的库的路径获取正确的值。当我试图调用DLL的方法时,它说
java.lang.reflect.InvocationTargetExceptio引起:java.lang.UnsatisfiedLinkError:jni.Test.nativeTest()V
请帮助我解决上述问题,在dll我只打印一个" Hello"。它没有通报。
更新C ++代码: 在Test class&上使用javah生成的头文件因此写了cpp代码。
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jni_Test */
#ifndef _Included_jni_Test
#define _Included_jni_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: jni_Test
* Method: nativeTest
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_jni_Test_nativeTest
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
#include "stdafx.h"
#include <jni.h>
#include <iostream>
#include <stdio.h>
#include "jni_Test.h"
using namespace std;
JNIEXPORT void JNICALL Java_jni_Test_nativeTest
(JNIEnv *, jclass){
std::cout << "Hello JNI !!";
printf("Hello JNI Print !!!!!");
return;
}