Solved

access native(existing) dll's from java

Posted on 2000-04-14
4
344 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 46
strDist challenge 35 84
groovy example issue 10 66
Java Server Faces parameter pass? 6 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now