Solved

access native(existing) dll's from java

Posted on 2000-04-14
4
352 Views
Last Modified: 2013-11-23
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) ?
0
Comment
Question by:lior_shapira
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2715739
otherwise ;)
use ms.* microsoft java packages
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2716240
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 {
            System.loadLibrary("native.dll");
        }
    }

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



0
 

Author Comment

by:lior_shapira
ID: 2716273
as i said, i need to use an EXISTING dll, i dont need new native code
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 2716802
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, java.io, and java.net, 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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question