Click here to Skip to main content
15,891,940 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
Iam getting an Exception "UnsatisfiedLinakge error" ie,

Exception in thread "main" java.lang.UnsatisfiedLinkError: test.HelloWorld.print()V
at test.HelloWorld.print(Native Method)
at test.HelloWorld.main(HelloWorld.java:24)

Iam using Netbeans7.1 and Eclipse to link Java and C++

Please see the below code..

Java Part
*************
Java
package test;
public class HelloWorld
{    
    static
    {
       // System.load("C:\\eclipse\\ws\\HelloWorld\\Debug\\libHelloWorld.dll");
        System.loadLibrary("libHelloWorld");
    }
    private native void print();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
          
       new HelloWorld().print();
                       
    }
}


C++ - ECLIPSE
***********

"hello_jni.h"
****************
C++
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class helloworld_HelloWorld */

#ifndef _Included_test_HelloWorld
#define _Included_test_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     helloworld_HelloWorld
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_test_HelloWorld_print(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


"hello_jni.cpp"
******************
C++
include "hello_jni.h"
using namespace std;
/*
 * Class:     hello_jni.cpp
 * Method:    print
 * Signature: ()V
 */

JNIEXPORT void JNICALL Java_test_HelloWorld_print(JNIEnv * env, jobject jobj)
{
	std::cout<<"Iside C++ native method HELLOOOOOOOOOOOOOOOOOOO";
	return;
}


Please Help
Thanks in advance.
Posted
Updated 27-Feb-15 3:25am
v9
Comments
Richard MacCutchan 26-Feb-15 11:36am    
How did you generate your C++ header file?
Member 11482626 26-Feb-15 12:55pm    
using javah command ie, javah test.HelloWorld then implemented c++ file

I haven't touched JNI for a while, but I believe your C++ library is statically linked to jni.dll. When your program is running it cannot load your DLL because the system cannot find jni.dll in the default search path and as a result your DLL cannot be loaded. So check that.
 
Share this answer
 
I suspect that you do not have a default path for java.library.path so java cannot locate the DLL. I just built your project, copied the DLL to the same directory as the class file ...
 Directory of C:\Users\Richard\Documents\Java\jni2\test

27/02/2015  14:45    <dir>          .
27/02/2015  14:45    <dir>          ..
27/02/2015  14:28               463 HelloWorld.class
27/02/2015  14:27               481 HelloWorld.java
27/02/2015  14:44            37,376 libHelloWorld.dll
               3 File(s)         38,320 bytes
               2 Dir(s)  123,847,589,888 bytes free
</dir></dir>

... and typed the command ...
java -Djava.library.path=C:\Users\Richard\Documents\Java\jni2\test test/HelloWorld

receiving the result
Iside C++ native method HELLOOOOOOOOOOOOOOOOOOO
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900