Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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 {

        }

    }

}


calling the above method from Main.java
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);       
    }

 }


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
java.lang.reflect.InvocationTargetExceptio Caused by: java.lang.UnsatisfiedLinkError: jni.Test.nativeTest()V


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.

C++
Header File : jni_Test.h


C++
/* 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#
C++ File : jni_Test.cpp


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


What I have tried:

Tried many ways , bt getting always "UnsatisfiedLinkError".
Posted
Updated 1-Nov-16 22:23pm
v3
Comments
Richard MacCutchan 31-Oct-16 7:50am    
Where is the code for the DLL?
Member 12373848 2-Nov-16 4:26am    
I have updated the Dll (C++ code) above , Please review the same and let me know if I missed anyhthing.
Richard MacCutchan 2-Nov-16 4:58am    
I cannot see anything wrong with your code, and have compared it to an old project of mine, but it all looks correct. The only difference is that you have declared the native method as static, whereas my version was not. Unfortunately I cannot test this as my version of Java is 64 bit, but VS is 32.
Richard MacCutchan 2-Nov-16 5:52am    
I have managed to recreate the problem and it is definitely caused by your native method being declared static. Change the declaration, and the call as follows:

public native void nativeTest(); // remove static keyword

public static void NativeTestJni() {
try {
System.out.println("\n Called Frm MEthod");
new Test().nativeTest(); // call native function on an object

Don't forget to rerun javah, and rebuild the DLL afterwards.
Member 12373848 7-Nov-16 23:22pm    
thank you so much for your help , i am already working on this. Will let you know with the updates

1 solution

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.
 
Share this answer
 
Comments
Member 12373848 2-Nov-16 4:25am    
Thank you for your guidance. I have generated header file using Javah & accordingly i have wrote C++ code. I have updated the dll(C++) code part in above section. Please review the same and let me know if i have missed anything
KarstenK 2-Nov-16 6:06am    
Because these calls are "expensive" your Test object should be a class member and create some steps before. The JNI has a lazy runtime!!!
Richard MacCutchan 8-Nov-16 4:03am    
No, the problem is that the native method was declared static, so there will not be any this keyword.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900