Solved

access native(existing) dll's from java

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

636 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