EDIT3: It is defiantly the compiler flags causing the problem, if I compile from the command line using Microsoft’s compiler it works fine. Does anyone know what I need to change in code::blocks to fix this issue?
When I run my code I am getting a UnsatisfiedLinkError. The load step works fine, i am getting the error when it actually calls the code.
Started with the path to my dll
java -Djava.library.path=E:\Java\JNIHellowWorld\PasswordGenHW\bin\Debug -jar dist\JNIHellowWorld.jar
The Exception
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnihellowworld.Main.HelloWorld()Ljava/lang/String;
at jnihellowworld.Main.HelloWorld(Native Method)
at jnihellowworld.Main.main(Main.java:16)
Java Code
package jnihellowworld;
import java.io.IOException;
public class Main {
public native String HelloWorld() throws Error;
public static void main(String[] args) throws IOException {
try
{
System.loadLibrary("PasswordGenHW");
String test = new Main().HelloWorld(); //errors on this line
System.out.println(test);
System.in.read();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
C++ header
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jnihellowworld_Main */
#ifndef _Included_jnihellowworld_Main
#define _Included_jnihellowworld_Main
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: jnihellowworld_Main
* Method: HelloWorld
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_jnihellowworld_Main_HelloWorld
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
C++ cpp
#include "jnihellowworld_Main.h"
#include <jni.h>
JNIEXPORT jstring JNICALL Java_jnihellowworld_Main_HelloWorld
(JNIEnv *env, jobject obj)
{
return env->NewStringUTF("Hello world!");
}
I am using Code::Blocks and GCC to complle the dll.
EDIT: Here is the dumpbin /exports of the dll
2 1 000011D8 Java_jnihellowworld_Main_HelloWorld@8
EDIT2: simplified project to duplicate error using system.loadLibrary()
I ended up just compiling by hand using the Microsoft’s compiler.