1

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

Narayan
  • 1,189
  • 6
  • 15
  • 33
  • 1
    Possible duplicate of http://stackoverflow.com/questions/3719065/java-calling-dll-from-a-local-applet-im-doing-something-wrong or http://stackoverflow.com/questions/1713403/calling-a-dll-from-an-applet-via-jni Check this as startup : http://www.javaworld.com/jw-07-1998/jw-07-java-win32.html – rkosegi Mar 07 '12 at 08:32

2 Answers2

3

You should use JNI. Take a look on this article.

  1. Make sure that the environment variable, CLASSPATH, contains a reference to "[WINDIR]\Java\Classes\Classes.zip" and "C:" (assuming that C: is your development drive).
  2. Make sure that your "[SDK-Java]\Bin" directory is included in your path (for JavaH, JVC, and JView).
  3. Make sure that Visual C++ is properly set up for command-line use. See your Visual C++ documentation for details.
  4. Write your Java code:

    public class TestJNI {
       public native void greetings();
    
       static {
          System.loadLibrary("greet");
       }
    
       public static void main(String args[]) {
          new TestJNI().greetings();
       }
    }
    
  5. Compile the Java file:

    jvc TestJNI.java
    
  6. Run JavaH on the generated class file:

    javah -jni TestJNI
    
  7. 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++!");
    }
    
  8. Compile the C/C++ code:

    cl greet.cpp -Ic:\sdk-java.31\include -Fegreet.dll -MD -LD
    
  9. Test the application:

    jview TestJNI
    
MByD
  • 135,866
  • 28
  • 264
  • 277
aviad
  • 8,229
  • 9
  • 50
  • 98
0

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.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Hi @Andrew I just wanted to say, for the benefits of those reading this question, that JWS is not a miracle cure. It has a nasty bug where the cache will get corrupted, and then it just won't load your app anymore until the cache is manually cleared. The bug already existed before Sun was bought buy Oracle if I remember well, and they just don't care. We have a support person spending most of his day explaining to accountants on the phone how to clear their Java cache (and many just don't get it). Not good! – Sebastien Diot Jun 04 '12 at 18:42
  • *"JWS is not a miracle cure."* Since that is intuitively obvious, I don't know why you bothered mentioning it. *"It has a nasty bug"* All non-trivial apps. have bugs. – Andrew Thompson Jun 05 '12 at 00:39
  • "All non-trivial apps. have bugs" Yes. The problem is just in this case that *they don't fix it*. I think that any bug in a framework/tool you rely on to run/deploy your product, which is so bad that it is likely to cost you many customers, and which *you cannot fix or work around yourself*, is worth mentioning to the prospective user. I wasn't criticizing you, but rather WebStart; I'm sorry you seem to be taking it personally. – Sebastien Diot Jun 05 '12 at 11:09