从javafx应用程序调用DLL方法时出现问题 [英] Issue in calling a method of DLL from javafx application

查看:621
本文介绍了从javafx应用程序调用DLL方法时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个JavaFX应用程序&一个C ++ DLL我的方法我想从JavaFX(java代码)调用。所以,我把dll放在项目目录中,在运行时获取工作目录,因此创建了dll的路径,将相同的路径值设置为java.library.path&然后在静态块中加载dll。在我尝试访问dll的方法加载后,每次我得到不满意的LinkError。



请指导我上述问题。 JavaFX JNI应用程序是否有效??



以下代码:

一个文件是jni包&一个是xyz包。

I am developing a JavaFX application & a C++ dll whose method i want to call from JavaFX (java code). So, i have placed the dll in project directory, getting the working directory during runtime , hence creating the path to dll , setting the same path value to "java.library.path" & then loading the dll in static block. After loading when i am trying to access method of dll , everytime I am getting "Unsatisfied LinkError".

Please guide me with the above issue. Whether JavaFX JNI application works ??

Following is code :
one file is in jni package & one is xyz package.

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 {

        }

    }

}





从Main.java调用上述方法



calling the above method from Main.java

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的方法时,它说



I am getting correct value for the path of library which I am printing. Bt when I am trying to call method of DLL it says

java.lang.reflect.InvocationTargetExceptio Caused by: java.lang.UnsatisfiedLinkError: jni.Test.nativeTest()V





请帮我解决上述问题,在dll我只打印一个你好。它已经准备好了。



更新C ++代码:在Test class& amp;上使用javah生成头文件因此写了cpp代码。





Please help me with the above issue , in dll i am only printing a "Hello". Its nt comimg up.

Updating the C++ code : generated header file using javah on Test class & hence wrote cpp code accordingly.

Header File : jni_Test.h







/* 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







C++ File : jni_Test.cpp







#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;
}





我的尝试:



尝试了很多方法,但总是得到UnsatisfiedLinkError。



What I have tried:

Tried many ways , bt getting always "UnsatisfiedLinkError".

推荐答案

被调用的函数没有用你的C dll中的JNI正确导出,也许它是只有错误的数据类型作为参数。第一个是总是 JNI指针。



这是一个很好看的教程所有内容都有解释。



JNI就是这样工作的,所以要小心以这种方式实现c-dll。
The called function isnt properly exported with JNI from your C dll, maybe it is only the wrong data type as parameters. The first is ALWAYS the JNI pointer.

Here is a fine looking tutorial where all is explained.

JNI works that way, so take care to implement the c-dll in this manner.


这篇关于从javafx应用程序调用DLL方法时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆