I have my own built-in Eclipse plugin where I need to call the C ++ dll.
I tried to do this in two steps: 1. outside my Eclipse plugin through the main Java program that calls the C ++ dll 2. try to connect it to my plugin (this is the problem)
- outside of the Eclipse plugin.
The main Java code is 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 by the command: "C: \ Program Files \ Java \ jdk1.6.0_34 \ bin \ javac" HelloWorld.java
Then I made h the HelloWorld.h file for the C ++ dll using:
"C: \ Program Files \ Java \ jdk1.6.0_34 \ bin \ javah" HelloWorld
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 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 dll
When I run the main java program ... everything works fine!
- try connecting it to my Eclipse plugin (this is the problem)
I created a class that 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);
and name it as follows:
HelloWorld hw = new HelloWorld(); result = hw.print("Hi from Eclipse");
Then I get this error on hw.print (DLL loading completed):
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.