1

I understand this question has been asked many times, I have read many post without positive outcome, thus seeking assistance.

Original source is Java Programming with JNI

I am getting the following error

Exception in thread "main" java.lang.UnsatisfiedLinkError: no libSample1 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at Sample1.main(Sample1.java:10)

Source code is as follows

Sample1.java

public class Sample1
{
public native int intMethod(int n);

public static void main(String[] args)
{
    System.loadLibrary("libSample1");
    Sample1 sample  = new Sample1();
    int     square  = sample.intMethod(5);

    System.out.println("intMethod " + square);
}
 }

Sample1.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Sample1 */
#ifndef _Included_Sample1
#define _Included_Sample1
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_Sample1_intMethod
  (JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif

Sample1.cpp

#include "Sample1.h"
#include <string.h>

JNIEXPORT jint JNICALL Java_Sample1_intMethod(JNIEnv *env, jobject obj, jint num)
{
return num * num ;
}

OS: Ubuntu 12.04 LTS

Compile command

g++ -shared -fPIC -I/usr/lib/jvm/jdk1.6.0_45/include -I/usr/lib/jvm/jdk1.6.0_45/include/linux Sample1.cpp -o libSample1.so

I also have tried

java -Djava.library.path=/home/userid/java_proj/example Sample1

Thank you for the help in advance

Update -> Answer: Javah -jni Sample1 fixed the problem

Thanks

Mahendra Gunawardena
  • 1,956
  • 5
  • 26
  • 45
  • How does this relate to Android? Is the `.so` file executable? – Peter Lawrey Sep 04 '13 at 22:05
  • 1
    did you look at [this][1]? I think this addresses your problem. [1]: http://stackoverflow.com/questions/3950635/how-to-compile-dynamic-library-for-a-jni-application-on-linux?rq=1 – HughB Sep 04 '13 at 22:32

0 Answers0