I need to call a method in dll using Java Applet. The dll is written in C++.
Whether it is possible, if so what are the things needed.
can anyone provide sample api
I need to call a method in dll using Java Applet. The dll is written in C++.
Whether it is possible, if so what are the things needed.
can anyone provide sample api
You should use JNI. Take a look on this article.
Write your Java code:
public class TestJNI {
public native void greetings();
static {
System.loadLibrary("greet");
}
public static void main(String args[]) {
new TestJNI().greetings();
}
}
Compile the Java file:
jvc TestJNI.java
Run JavaH on the generated class file:
javah -jni TestJNI
Write the C/C++ code based on the generated header file:
#include "TestJNI.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_TestJNI_greetings(JNIEnv *env,jobject jobj) {
printf("Hello from Visual C++!");
}
Compile the C/C++ code:
cl greet.cpp -Ic:\sdk-java.31\include -Fegreet.dll -MD -LD
Test the application:
jview TestJNI
See the linked threads by rkosegi re. 'trusted code' - important to understand. aviad has covered many details of one way to do it. This post will simply focus on deploying the natives.
The real problem with applets using natives is getting the natives installed in a place where the applet can access them. That is where deploying the applet using Java Web Start becomes useful. JWS can not only partition the download of natives according to OS & architecture (32/64 bit), but then make the natives available on the run-time class-path of the app., ready for loading.
As of the 'Next Generation' plug-in (Sun's 1.6.0_10+, for e.g.), JWS could deploy embedded applets (previously it could only launch them free-floating).
But then, why do you want an applet at all?
The better alternative is usually to launch a free-floating frame direct from a link (using JWS). The same security restrictions apply, but it is easier to deploy and a better user experience.