7

I need to call external DLL library function from Java code. I use Netbeans 7.2. My dll's functions are:

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

I'm following this example. But I don't know how declare my dll functions. And I found another link. But it doesn't work for me.

3
  • 7
    "It doesn't work for me" isn't nearly enough information for us to help you. You need to tell us what you've tried, and what the results were. Please read tinyurl.com/so-list Commented Feb 21, 2013 at 8:09
  • 1
    What does not work? Can you share some code of what you tried? Commented Feb 21, 2013 at 8:09
  • 1
    For Linux: stackoverflow.com/a/30635871/895245 Commented Jun 4, 2015 at 6:06

4 Answers 4

8

I stumbled upon the same problem of "calling DLL from Java" and first was frustrated about the complexity. Yet, there is an elegant solution (might also be interesting for the people over there in the processing.org habitat..) Given the rather "general" form of the question (maybe, downrating is not justified for that), I suppose, a rather easy-going solution would be indicated. In other words, a solution that avoids messing aronud with header files, extra conversions, etc., just as the source code is not necessarily available.

My recommendation for that would be JNA (https://github.com/twall/jna), which basically is a simplifying wrapper around JNI. It works great, type mapping is straightforward (e.g. pchar = lpcstr buffer -> string), though I am using it only towards Windows DLLs and my own C-style DLLs created using Delphi-Pascal. The only thing to consider is that return values should be exported through functions rather than "out" flagged reference variables. The question already points to a linked source that provides an example for that (so, answers around JNI may be misplaced here). Note that the link I provided also contains axamples for the transfer of arrays and pointers.

Sign up to request clarification or add additional context in comments.

Comments

2

You will need to use the Java Native Interface (JNI), which is a set of C/C++ functions that allow native code to interface with java code (i.e. receiving parameters from java function calls, returning results, etc). Write a wrapper C library that receive JNI calls and then call your external library.

For instance, the following function invokes a method updateHandlers on a native object (that is stored as long in 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);
}

The corresponding declarations in package.ClassName are:

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
}

Comments

1

I did write some time ago sample tutorial, maybe it will help.

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

1 Comment

This is a link-only answer, which is not acceptable in general.
1

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

The corresponding C method, would be:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.