How to call an external dll function from java code

I need to call an external library DLL from Java code. I am using Netbeans 7.2. My dll functions:

Boolean isValid(string word) List<String> getWords(String word) 

I follow this example. But I do not know how to declare my dll functions. And I found the link. But this does not work for me.

+6
source share
4 answers

I came across the same problem of "calling a DLL with Java" and was initially disappointed with the complexity. Nevertheless, there is an elegant solution (it may also be interesting for people where the habitat is process.org ..) Given the rather "general" form of the question (perhaps a overthrow is not justified for this), I believe a fairly simple solution will be indicated . In other words, a solution that allows messing with header files, additional conversions, etc., Just like the source code is not necessarily available.

My recommendation for this would be JNA ( https://github.com/twall/jna ), which is basically a simplifying wrapper around JNI. It works fine, type matching is simple (e.g. pchar = lpcstr buffer β†’ string), although I only use it for Windows DLLs and my own C-type DLLs created using Delphi-Pascal. The only thing to keep in mind is that return values ​​must be exported through functions, not "referenced" referenced variables. The question already points to a related source that provides an example for this (so answers around JNI may not be appropriate here). Please note that the link I provided also contains axograms for moving arrays and pointers.

+6
source

You will need to use the Java Native Interface (JNI) , which is a set of C / C ++ functions that allow you to use your own code for an interface with java code (i.e. get parameters from calls to Java functions, return results, etc. d.). Write a shell C library that accepts JNI calls and then calls your external library.

For example, the following function calls the updateHandlers method on its own object (which has been stored for so long on the Java side).

 class MyImpl { void updateHandlers(JNIEnv *env) { this->contentHandler = ....; } } JNIEXPORT void JNICALL Java_package_Classname_updateHandlers0 (JNIEnv *env, jobject obj, jlong ptr) { ((MyImpl*)ptr)->updateHandlers(env); } 

Relevant declarations in package.ClassName:

 private long ptr; //assigned from JNI public void updateHandlers() { if (ptr==0) throw new NullPointerException(); updateHandlers0(ptr); } private native void updateHandlers0(long ptr); static { try { /*try preloading the library external.dll*/ System.loadLibrary("external"); } catch (UnsatisfiedLinkError e) { /*library will be resolved when loading myjni*/ } System.loadLibrary("myjni"); //load myjni.dll } 
+2
source

I wrote a sample tutorial some time ago, maybe this will help.

http://wendro.blogspot.com/2010/03/jni-example-eclipse-dev-cpp.html

+1
source

You declare your own functions in java ( native private ... ) with the signature you need; then run javah (the tool that comes with the JDK) to generate your own headers. A List<String> (actually a List , due to type erasure) is a jobject in native code.

Corresponding Method C:

 JNIEXPORT jobject JNICALL package_Classname_getWords(JNIEnv *env, jobject jobj, jstring word) 

I think it would be easier to return an array of jobjectArray strings and create an instance of List in java from the returned values. See this example .

+1
source

All Articles