4

I have a test case where I am trying to access the C code from my Java program using JNI. Steps involved are as follows :

1. A Java program calling the native methods :

public class RunnerClass{
    public native void win32_svc_install();
    static{
        System.loadLibrary("testDll");
                System.out.println("library loaded successfully");   
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new RunnerClass().win32_svc_install();
    }

}

2. Now after the .class file gets generated and from that corresponding .h file created, I put up the native method implementation inside the .c file.

    /* DO NOT EDIT THIS FILE - it is machine generated */
//RunnerClass.h

#include <jni.h>
/* Header for class RunnerClass */

#ifndef _Included_RunnerClass
#define _Included_RunnerClass
#ifdef __cplusplus
extern "C" {
#endif/*
 * Class:     RunnerClass
 * Method:    nx_win32_svc_install
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_RunnerClass_win32_1svc_1install
  (JNIEnv *, jobject);#ifdef __cplusplus
}
#endif
#endif

The RunnerClass.c file has the implementation for the native method inside it. What exactly this method will do is call the ServiceManager of the windows to make use of it. My Java program needs to perform these actions.

Now the problem arises after the testDll.dll gets created. Before interpreting the Java code, I set the library path for the required library(testDll) in the java.library.path.

Now when I run my program, my library gets loaded but it throws UnsatisfiedLinkError to the native method. The exact error is as follows :

Exception in thread "main" hello ,java.lang.UnsatisfiedLinkError: RunnerClass.win32_svc_install(ILjava/lang/String;)V
    at RunnerClass.win32_svc_install(Native Method)
    at RunnerClass.main(MainWs.java:58)

I did a lot of research and understood by far that the exception is thrown because the program is not able to find the implementation of the native method in the library being loaded.

Abhishek
  • 1,974
  • 5
  • 31
  • 67
  • 1
    What's the function `parsecmdline()`? – millimoose Mar 16 '13 at 13:01
  • It looks like the native method is trying to call a Java method, and the Java method can't be found. Do you have the source code for the native method? – Joni Mar 16 '13 at 13:06
  • @millimose : It was a mistake. It's been edited now. – Abhishek Mar 16 '13 at 13:08
  • @Joni : I do not get ypu comment? My JAVA program has a native method declaration whose definition is inside a [.c] file that after build creates a [.dll] file. No native method is calling the java method. – Abhishek Mar 16 '13 at 13:11
  • 1
    How are you running your app, via commandline or IDE? How exactly are you setting the property `java.library.path`? – Byron Mar 16 '13 at 13:13
  • Also: if all you need is run your Java program as a service, use this: http://wrapper.tanukisoftware.com/doc/english/index.html – millimoose Mar 16 '13 at 13:16
  • [JNA](https://github.com/twall/jna#readme) should also be a less terrible option for most cases where you'd use JNI. It should come with a complete Win32 binding. – millimoose Mar 16 '13 at 13:17

1 Answers1

1

The exception does somehow not match your code. In Java, you declare the function as

public native void win32_svc_install();

In c++, you declare the function as

JNIEXPORT void JNICALL Java_RunnerClass_win32_1svc_1install (JNIEnv *, jobject);

I think it should be declared as

JNIEXPORT void JNICALL Java_RunnerClass_win32_svc_install (JNIEnv *, jobject);

But besides the strange "1" in the c++ declaration, there seems to be another problem. Both functions are declared correctly as a void function with zero arguments.

But your exception states, that it is looking for a void function of the same name, but with an integer and a string argument:

RunnerClass.win32_svc_install(ILjava/lang/String;)V

When looking at your code I can't imagine why. I tried to reproduce it by renaming one of my C++ functions; the following unsatisfied link exception correctly stated the defined parameters.

user2328447
  • 1,807
  • 1
  • 21
  • 27