2

This is the Native.cpp :

// Native.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#define ALLEGRO_NO_MAGIC_MAIN
#include <stdio.h>
#include <string>
#include <windows.h>
#include "generic_interface.h"
#include "NativeC.h"

using namespace std;

// Some useful defines I liked from Sun's stuff
#define JNIEXPORT __declspec(dllexport) 
#define JNICALL __cdecl
#define jint long


typedef ExportedClass* (__cdecl *exported_class)();
HINSTANCE temptDLL;
ExportedClass** importedClasses;
char** classNamePerIndex;
int libraryLength = 0;

JNIEXPORT void JNICALL _JAVA_initiate(HNative *self, jint libraryLength) {
    importedClasses = new ExportedClass*[libraryLength];
    classNamePerIndex = new char*[libraryLength];
}

and the Java class that implements and loads this native dll generated from the above Native.cpp file is like :

public class Native {
    // guess?
  native public void initiate(int libraryLength);


    // Loads the file Native.DLL at run-time
  static {
    System.loadLibrary("Native");
  }

    // Constructor
  public Native()
  {
  }

}

But while calling

(new Native()).initiate(1);

I get this run time error :

Exception in thread "main" java.lang.UnsatisfiedLinkError: Native.initiate(I)V

I have tried to rename _JAVA_initiate to JAVA_initiate and NATIVE_initiate and _JAVA_NATIVE_inititate and even JAVA_NATIVE_inititate, but It did not work still

The library is loading perfectly fine, just while calling the native method, it is giving link error.

EDIT: Below listed is the NativeC.h that is already included in the Native.cpp

/*  DO NOT EDIT - automatically generated by javah  */
#include "Native.h"

/*  Header for class Native  */

#ifndef _Included_Native
#define _Included_Native

typedef struct ClassNative {
#pragma pack(push,4)
    int32_t MSReserved;
    struct Hjava_lang_String * string_;
    /*boolean*/ long boolean_;
    /*byte*/ long byte_;
    /*char*/ long char_;
    double double_;
    float float_;
    long int_;
    int64_t long_;
    /*short*/ long short_;
    struct Hjava_lang_String * w;
    long x;
    long y;
#pragma pack(pop)
} ClassNative;
#define HNative ClassNative

#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) void __cdecl _JAVA_initiate (struct HNative *, long);
__declspec(dllexport) void __cdecl _JAVA_loadLibraryAndInitiate (struct HNative *, struct     Hjava_lang_String *);
__declspec(dllexport) long __cdecl _JAVA_evaluateLibrary (struct HNative *, struct             Hjava_lang_String *, struct Hjava_lang_String *);
#ifdef __cplusplus
}
#endif
#endif
user1803112
  • 83
  • 1
  • 5

2 Answers2

0

You need to use javah to generate the signature for your function, as the one you're using is missing a number of things. In particular, the JNI environment, which gets passed as an argument to every function.

Dave S
  • 20,507
  • 3
  • 48
  • 68
  • Hi, I did not include the file in the question previously, but now I have updated it. I am using HNative instead of JNI as this http://www.pacifier.com/~mmead/cs510jip/jni/ post suggests to use HNative for Windows. – user1803112 Nov 07 '12 at 06:10
0

You need to implement the method declared in Native.h that you generated using javah Normally a class has a package and this is part of the method name.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hi, I did not include the file in the question previously, but I do include the NativeC.h that has all the functions declared. – user1803112 Nov 07 '12 at 06:08
  • And if you define the method declared in the `.h` file it will work. – Peter Lawrey Nov 07 '12 at 07:02
  • If you are doing this but its still failing you have a different problem. For example, you can't load a 32-bit DLL into a 64-bit JVM. – Peter Lawrey Nov 07 '12 at 07:04
  • I worked out every thing and it finally worked. But now I am having a very weird issue. When I call the initiate function from Java, it works, but as soon as I call any second function, it gives link error. There is nothing wrong in the C++ code, as I moved every single line of C++ code inside the initiate function and the function still ran, while keeping only a printf statement in the second function, and it still gave error. – user1803112 Nov 27 '12 at 08:42