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
- Here is the file structure in eclipse
- Here is the the java build path including the native library in eclipse
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