2

I'm trying to learn how to use JNI and I'm using the code from this thread How to compile dynamic library for a JNI application on linux?

HelloJNI.cpp:

#include <jni.h>
#include "HelloJNImpl.h"
#include "HelloJNI.h"

JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {
    sayHello();
    return;
}

HelloJNI.h:

#ifndef HELLOJNI_H_
#define HELLOJNI_H_

#ifdef __cplusplus
        extern "C" {
#endif

JNIEXPORT void JNICALL Java_Hello_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}

#endif

#endif

HelloJNImpl.cpp

#include <iostream>
#include <stdio.h>

using namespace std;

void sayHello () {
    cout << "Hello World!" << endl;
    return;
}

HelloJNImpl.h

#ifndef HELLOJNIMPL_H_
#define HELLOJNIMPL_H_

#ifdef __cplusplus
        extern "C" {
#endif
        void sayHello();

#ifdef __cplusplus
        }

#endif

#endif

Next, I've compiled my SO using

g++ -I/opt/java/jdk1.7.0_51/include -I/opt/java/jdk1.7.0_51/include/linux -fPIC -o libHelloJNI.so -shared HelloJNI.cpp

Which gives me the libHelloJNI.so

I also get the expect results when I use nm

nm libHelloJNI.so | grep say
0000000000000675 T Java_Hello_sayHello
                 U sayHello

But when I go to test this from my terminal using

java -Djava.library.path=. sayHello
Error: Could not find or load main class sayHello

It say's that it can't find sayHello. I think I'm missing something small here and can't find it. Can someone tell me where I might be going wrong with this code?

Community
  • 1
  • 1
user2229544
  • 477
  • 2
  • 10
  • 23
  • The only difference I can find is in the output of nm from the linked post vs is mine. In the other post sayHello is marked as T where mine is marked as U – user2229544 Mar 05 '14 at 02:07

0 Answers0