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 {
}
}
}
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);
}
}
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
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
#include <jni.h>
#ifndef _Included_jni_Test
#define _Included_jni_Test
#ifdef __cplusplus
extern "C" {
#endif
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;
}
What I have tried:
Tried many ways , bt getting always "UnsatisfiedLinkError".