access native(existing) dll's from java

I need to access an existing dll(i dont have the source) from java, and call a procedure "char[] dosomething(char[])", how do i do this through JNI (or otherwise) ?
Who is Participating?
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
JNI is essentially a bridge that the JVM uses between Java application code and native application code. It was introduced in release 1.1.

Earlier releases of Java technology included a native method interface that allowed Java applications to call functions written in other languages such as C or C++. Implementations of packages such as java.lang,, and, relied heavily on this native interface. Unfortunately, there were several problems with this interface including memory layout issues and assumptions about garbage collection. JNI was designed to solve these problems.

As of release 1.2, the JDK no longer uses the old-style native method interface. Everything is based on JNI. JNI is the native interface standard to which applications programs should be written. The old-style interface will not (and cannot) be supported in future VM implementations.

The upshot of this is that you cannot call native code directly from Java without benefit of JNI. So essentially, you have two possible strategies:

1) Write a JNI bridge between Java and your existing dll. This allows you to avoid modifying the existing (and presumed working) code which you may need to keep intact anyway if you are currently calling it from other languages.

2) Modify the existing library to provide a JNI-compliant interface. This way you will only have a single dll to maintain. This is not an option, of course, if you need the current library intact or you do not have source.

I hope I've clarified this point for you.

Best regards,
Jim Cakalic
otherwise ;)
use ms.* microsoft java packages
Jim CakalicSenior Developer/ArchitectCommented:
I would _really_ recommend reading the JNI thread of the Java Tutorial. It is free and can be read online or downloaded. This will give you a decent overview. If you need more info, I'd suggest buying either the Addison-Wesley JNI Programmer's Guide or Essential JNI (can't remember the publisher).

To get you started, here is an extremely simplified explanation of what you need to do for JNI access to an existing dll.

First, write a Java class encapsulating the access from the Java side. This class will declare one or more native methods and (likely) have a static initialization block to load the library:

    // you can put it in any package you want
    package util.native;
    public class NativeAccess {
        public native char[] doSomething(char[]);

        static {

Compile the class. Run javah to produce a C/C++ header file. In this case, if you run it correctly, javah will produce util_native_NativeAccess.h.

Now write your C or C++ file to implement this native method.

    #include "util_native_NativeAccess.h"
    JNIEXPORT jcharArray JNICALL Java_util_native_NativeAccess_doSomething(
JNIEnv *env,
jobject java_this,
jcharArray param) {
    // method implementation

Now compile the native code as a shared library, put it in a directory named by your PATH variable, and you are ready to run.

There are special rules for using java arrays in native code. The Java Tutorial briefly addresses this and provides some sample code. I know its all a little confusing at first. Find some samples (in the tutorial, the books, or from searching the web) and study them. IMHO, this is the best way to see how it all fits together.

Best regards,
Jim Cakalic

lior_shapiraAuthor Commented:
as i said, i need to use an EXISTING dll, i dont need new native code
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.