Exception in thread "main" java.lang.UnsatisfiedLinkError: RunnerClass.parsecmdline (ILjava / lang / String;) V

I have a test case where I am trying to access C code from my Java program using JNI. The following steps apply:

1. Java program calling its own 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 creating the .class file and creating the corresponding .h file, I turned on the implementation of the built-in method 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 an implementation of the built-in method inside it. What exactly this method will do is call the ServiceManager from windows to use it. My Java program should do this.

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

Now, when I run my program, my library loads, but it throws an UnsatisfiedLinkError on its own 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 realized that the exception was thrown because the program could not find the implementation of its own method in the loadable library.

+4
source share
1 answer

The exception does not match your code in some way. In Java, you declare a function as

 public native void win32_svc_install(); 

In C ++, you declare a 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 weird β€œ1” in the C ++ declaration, there seems to be another problem. Both functions are declared correctly as a void function with null arguments.

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

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

When you look at your code, I cannot imagine why. I tried to reproduce it by renaming one of my functions in C ++; The following unsuccessful link exception set certain parameters correctly.

+1
source

All Articles