J'ai un propre plugin Eclipse de construction où j'ai besoin d'appeler une DLL C++.Plugin Eclipse, appel C++ dll
J'ai essayé de le faire en deux étapes: 1. en dehors de mon Eclipse plugin via Java MainProgram appelant le C++ dll 2. Essayez d'obtenir dans mon plugin (ce qui est là le problème)
- en dehors du plugin Eclipse.
Code Java principal 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);
}
}
Compilé via la commande: "C: \ Program Files \ Java \ jdk1.6.0_34 \ bin \ javac" HelloWorld.java
Puis je fis un HelloWorld.h h fichier pour le C++ dll via:
"C: \ Program Files \ Java \ jdk1.6.0_34 \ bin \ javah" HelloWorld
Le h fichier ressemble à ceci:
#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
Maintenant, le C++ de CLibHelloWorld.cpp dll:
#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);
}
Construire la dll
Quand je lance le programme principal java ... tout fonctionne très bien!
- essayer de le faire dans mon plugin Eclipse (ce qui est là le problème)
je fait une classe qui devrait appeler le dll C++:
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());
}
}
}
et l'appeler comme cela:
HelloWorld hw = new HelloWorld();
result = hw.print("Hi from Eclipse");
Ensuite, je reçois cette erreur sur hw.print (la charge de la DLL est terminée):
java.lang.UnsatisfiedLinkError: org.eclipse.ui.examples.recipeeditor.support.HelloWorld.print (Ljava/lang/String;) Ljava/lang/String;
Une longue histoire, mais comment puis-je le résoudre?
Merci.
Vous devez placer la DLL à la bonne place et indiquer à Eclipse où elle se trouve. http://stackoverflow.com/questions/661320/how-to-add-native-library-to-java-library-path-with-eclipse-launch-instead-of – Aubin
Oui. Au début, l'instruction loadlibrary a échoué, mais j'ai corrigé cela en ajoutant un chemin de bibliothèque à Eclipse. Après que la déclaration de loadlibrary est allée oke. – kahoona