• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JNI Error

 
Aashiq Babu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An error in creating JNI

This is my java file and i have created the header file HelloNative.j using the commang javah -jni HelloNative. Below is the header file

class HelloNative
{ public static native void greeting();
static
{ System.loadLibrary("greeting");
}
}



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

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: hello
* Signature: (Ljava/lang/String V
*/
JNIEXPORT void JNICALL Java_HelloWorld_hello
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

I have included the header file which was created above

#include "HelloNative.h"
#include <stdio.h>
#include <jni.h>
JNIEXPORT void JNICALL Java_HelloNative_greeting
(JNIEnv* env, jclass cl)
{ printf("Hello world!\n");
}


class HelloNativeTest
{ public static void main(String[] args)
{ HelloNative.greeting();
}
}

i have complied the C file using
cc HelloNative.c -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -shared -fpic -o HelloNative.so

I have created the HelloNativeTest.java to call the C function and when i compile i am getting the error

java HelloNativeTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: no greeting in java.l
ibrary.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:834)
at HelloNative.<clinit>(HelloNative.java:9)

I am getting this error. I have set the LD_LIBRARY_PATH as follows

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH


Pls help me to slove this problem
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you did System.loadLibrary("greeting"), but your compilation made a shared library called "HelloNative.so". That's not going to work. You need to get the name of your shared library right. I can't remember exactly how they're named on Linux, but it's going to be something with "greeting" in the name. Maybe "libgreeting.so"?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter's advice is spot on.

Moving to "Other Java APIs", where JNI questions go.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic