aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes JNI Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JNI Error" Watch "JNI Error" New topic
Author

JNI Error

Aashiq Babu
Greenhorn

Joined: Mar 23, 2006
Posts: 1
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

Joined: Oct 30, 2001
Posts: 1970
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"?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Peter's advice is spot on.

Moving to "Other Java APIs", where JNI questions go.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JNI Error