This discussion is archived
2 Replies Latest reply: Sep 14, 2011 5:29 PM by EJP RSS

UnsatisfiedLinkError while calling C library from Java Programm

886017 Newbie
Currently Being Moderated
I am trying call C function from Java code but I am getting error.

Below is the code

package jnitest;

class HelloWorld {

    private native void print();

    public static void main(String[] args) {
        new HelloWorld().print();

    static {
I used following commands to compile and generate header files
$ javac -d build src/jnitest/ 
$ javah -d build -classpath build -jni jnitest.HelloWorld
Then jnitest_HelloWorld.h was generated and I renamed it to HelloWorld.h.
I implemented HelloWorld.h in HelloWorld.c.
Below is the code for both the files

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jnitest_HelloWorld */

#ifndef _Included_jnitest_HelloWorld
#define _Included_jnitest_HelloWorld
#ifdef __cplusplus
extern "C" {
 * Class:     jnitest_HelloWorld
 * Method:    print
 * Signature: ()V
JNIEXPORT void JNICALL Java_jnitest_HelloWorld_print
  (JNIEnv *, jobject);

#ifdef __cplusplus
#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"

Java_HelloWorld_print(JNIEnv *env, jobject obj)
  printf("Hello World!\n");
I used following commands to generate so file
gcc -m32 -fPIC -D_REENTRANT -I/opt/jdk1.6.0_19/include -I/opt/jdk1.6.0_19/include/linux -c HelloWorld.c
gcc -m32 -shared HelloWorld.o -o
after all the execution I get following directory struct in build directory
$ ls -R
HelloWorld.c  HelloWorld.h  jnitest

I am executing following command to run the program.
build$ java -D. jnitest.HelloWorld
but I am getting exception
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnitest.HelloWorld.print()V
     at jnitest.HelloWorld.print(Native Method)
     at jnitest.HelloWorld.main(
I pointed LD_LIBRARY_PATH to build directory and executed
java -classpath build jnitest.HelloWorld
but I am still getting the same error.
Please help to fix the issue.