2

I am running elementary OS 64-bit. Eclipse ADT.

Hello I am trying to get a string from C++ to java via JNI.

I am however receiving two problems depending on how I try and run my application.

  • error) following error when I run in eclipse

enter image description here

enter image description here

  • Here is the file structure in eclipse

enter image description here

  • Here is the the java build path including the native library in eclipse

enter image description here

Here are the files.

Controller.java

package sslarp.controller;

public class Controller {

    private native String getMyMac();       // returns the current machines mac address
    private native String getMyIp();        // returns the current machines ip address

    static {
        System.loadLibrary("getmacip");
    }

    private String myMac;
    private String myIp;

    public Controller() {

    }

    public void run() { 
        myMac = getMyMac();
        myIp = getMyIp();

        System.out.println("java: mac "+myMac+"\njava: ip "+myIp+"\n");     
    }

    public static void main(String[] args) {        
        System.out.println("java: starting");

        Controller controller = new Controller();
        controller.run();
    }
}

Controller.class was made via the javac command resides in /home/karl/workspace/sslarp/bin/sslarp/controller/Controller.class

sslarp_controller_Controller.h which is generated via the javah command

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

#ifndef _Included_sslarp_controller_Controller
#define _Included_sslarp_controller_Controller
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     sslarp_controller_Controller
 * Method:    getMyMac
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_sslarp_controller_Controller_getMyMac
  (JNIEnv *, jobject);

/*
 * Class:     sslarp_controller_Controller
 * Method:    getMyIp
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_sslarp_controller_Controller_getMyIp
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

getmacip.cpp I made this by myself

#include <jni.h>
#include <stdio.h>

JNIEXPORT jstring JNICALL Java_sslarp_controller_Controller_getMyMac
  (JNIEnv *jenv, jobject jobj)
{
    printf("c: getMyMac() invoked!\n");

    char str[] = "foo";    

    jstring jstr = jenv->NewStringUTF(str);
    return jstr;
}


JNIEXPORT jstring JNICALL Java_sslarp_controller_Controller_getMyIp
  (JNIEnv *jenv, jobject jobj)
{
    printf("c: getMyMac() invoked!\n");

    char str[] = "bar";    

    jstring jstr = jenv->NewStringUTF(str);
    return jstr;
}

libgetmacip.so made via the command karl@karl-vm:~/workspace/sslarp/bin$ g++ -fPIC -o libgetmacip.so -shared -I $JAVA_HOME/include -I $JAVA_HOME/include/linux getmacip.cpp -lc

I also an the following command karl@karl-vm:~/workspace/sslarp/bin$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/karl/workspace/sslarp/lib/ to add the library to the java path

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • 3
    `java.lang.UnsatisfiedLinkError` has to do with the native library not being found. It has nothing to do with returning a string to Java from C++. – PaulMcKenzie Nov 24 '14 at 17:36
  • I'd recommend to try google and/or java documentation before asking SO – user396672 Nov 24 '14 at 17:40
  • @PaulMcKenzie Alright! Any ideas to what I may have done wrong? I have my .class java file in bin/package/file.class and the .so, .cpp and .h are in same directory! – basickarl Nov 24 '14 at 17:42
  • Probably http://stackoverflow.com/questions/903530/how-should-i-load-native-libraries-for-jni-to-avoid-an-unsatisfiedlinkerror" contains an answer – user396672 Nov 24 '14 at 18:01
  • @KarlMorrison - Oh, and BTW, the code you posted will work once you get your native library issues straightened out. – PaulMcKenzie Nov 24 '14 at 19:34
  • 1
    Please also post your Java code. Let's see how you define the native method in your Java class. – yushulx Nov 25 '14 at 08:10
  • @yushulx updated the entire question with a lot more information! – basickarl Nov 25 '14 at 11:03
  • @KarlMorrison - You say you're using 64-bit Eclipse. Are you running a 64-bit JVM? If so, then the native library has to be 64-bit. – PaulMcKenzie Nov 25 '14 at 16:44
  • @PaulMcKenzie I got it working with C however, via the same procedure (nearly) – basickarl Nov 29 '14 at 20:15
  • @PaulMcKenzie I figured it out, forgot to include the header file in the .cpp file... DOH. – basickarl Nov 29 '14 at 21:05

1 Answers1

0

I forgot to include the code #include "sslarp_controller_Controller.h" in the getmacip.cpp file.

Basically I forgot to add the header to the .cpp file.

basickarl
  • 37,187
  • 64
  • 214
  • 335