I have an own build Eclipse plugin where I need to call a C++ dll.
I tried to do this in two steps : 1. outside my Eclipse-plugin via a Java mainprogram calling the C++ dll 2. try to get it into my plugin (this is where the problem lies)
- outside Eclipse plugin.
Main Java-code HelloWorld.java.
class HelloWorld {
//public native void print(); //native method
public native String print(String msg); //native method
static //static initializer code
{
System.loadLibrary("CLibHelloWorld");
}
public static void main(String[] args)
{
//HelloWorld hw = new HelloWorld();
//hw.print();
String result = new HelloWorld().print("Hello from Java");
System.out.println("In Java, the returned string is: " + result);
}
}
Compiled via command : "C:\Program Files\Java\jdk1.6.0_34\bin\javac" HelloWorld.java
Then I made a h-file HelloWorld.h for the C++ dll via :
"C:\Program Files\Java\jdk1.6.0_34\bin\javah" HelloWorld
The h-file looks like this :
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_HelloWorld_print
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
Now the C++ dll CLibHelloWorld.cpp :
#include "HelloWorld.h"
#include "jni.h"
#include "stdafx.h"
#include "tchar.h"
#import "..\ManagedVBDLL\bin\Debug\ManagedVBDLL.tlb" raw_interfaces_only
using namespace ManagedVBDLL;
JNIEXPORT jstring JNICALL Java_HelloWorld_print(JNIEnv *env, jobject thisObj, jstring inJNIStr) {
jboolean blnIsCopy;
const char *inCStr;
char outCStr [128] = "string from C++";
inCStr = env->GetStringUTFChars(inJNIStr, &blnIsCopy);
if (NULL == inCStr) return NULL;
printf("In C, the received string is: %s\n", inCStr);
env->ReleaseStringUTFChars(inJNIStr, inCStr);
return env->NewStringUTF(outCStr);
}
Build the dll
When I run the java mainprogram ... it all works fine !
- try to get it into my Eclipse plugin (this is where the problem lies)
I made a class which should call the C++ dll :
package org.eclipse.ui.examples.recipeeditor.support;
import org.eclipse.jface.dialogs.MessageDialog;
public class HelloWorld {
public native String print(String msg); //native method
static //static initializer code
{
try {
System.loadLibrary("CLibHelloWorld"); //$NON-NLS-1$
} catch (Exception e) {
e.printStackTrace();
MessageDialog.openInformation(null, "HelloWorld", "HelloWorld Catch: " + e.getMessage());
}
}
}
and call it like this :
HelloWorld hw = new HelloWorld();
result = hw.print("Hi from Eclipse");
Then I get this error on hw.print (the load of the dll is done) :
java.lang.UnsatisfiedLinkError: org.eclipse.ui.examples.recipeeditor.support.HelloWorld.print(Ljava/lang/String;)Ljava/lang/String;
A long story, but how can I solve it ?
Thanks.