?
Solved

access native(existing) dll's from java

Posted on 2000-04-14
4
Medium Priority
?
365 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 150 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

719 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