2

I am following this guide "How to Create a JNI with NetBeans":

Using the code from this SO question:

I have generated a .dll and package file but am unsure what I do with them; how do I tell java where to find them using NetBeans?

My java code:

package addingcontrollerstogui;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class AddingControllersToGUI extends Application {

    public native int intMethod(int i);

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
        Scene scene = new Scene(root);        
        stage.setScene(scene);
        stage.show(); 
        System.loadLibrary("Main");
        System.out.println(new AddingControllersToGUI().intMethod(2));
    }

My c code:

#include "AddingControllersToGUI.h"
jint JNICALL Java_addingcontrollerstogui_AddingControllersToGUI_intMethod
(JNIEnv * env, jobject object, jint param1) {
return param1 * param1;
}
Community
  • 1
  • 1

1 Answers1

1

You need to load the DLL into the program. Edit and insert the following into your java code, right before the native method:

static {
    System.load("YourNetbeansFolder\\YourProject\\dist\\YourNative.dll");
}

static{} will run this method when AddingControllersToGUI is first accessed (so before intMethod() is even called).

Obviously, the folder in that code would need to be modified to point to the location of your DLL.

When you actually build and distribute your program, you'll need to figure out how you want this DLL to be loaded. For instance, in Minecraft, the DLL/SO (SO is the "DLL" of UNIX-based OSs) files used are extracted into %APPDATA%/.minecraft/ (or ~/.minecraft in UNIX-like) and loaded from there.

For now, this should suffice...!

Sameer Puri
  • 987
  • 8
  • 20