Calling a DLL from an Applet via JNI

mcottle :

I have a "proof of concept" piece of work that crosses over into some unfamiliar territory. I'm tasked with connecting an EFTPOS machine to an application running as an applet in a browser on our intranet.

I've ignored the EFTPOS dll for the moment and created a simple JNI decorated DLL in my language of choice (Delphi) that just logs a string to a text file in c:\ and I can call it successfully from a local Java application.

However, when I create an applet to do the same thing, compile it into a .JAR, sign the JAR & try to call the method in the applet via Javascript on a web page it fails.

A senior Java guy I'm working with doesn't think it will be possible to get this to work because it's inherently "evil" to allow an applet to do this.

There is an entry you can put in a java.policy file to allow loadLibrary. as well as allPermission & I've tried a whole host of variations along those lines all to no avail producing the following error trace in the Java Console:

java.lang.ExceptionInInitializerError
  at app.TestApplet.LogAString(Unknown Source)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.plugin.javascript.JSInvoke.invoke(Unknown Source)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
  at sun.plugin.com.MethodDispatcher.invoke(Unknown Source)
  at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
  at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.plugin.com.DispatchImpl.invoke(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission loadLibrary.DLoggerImpl)
  at java.security.AccessControlContext.checkPermission(Unknown Source)
  at java.security.AccessController.checkPermission(Unknown Source)
  at java.lang.SecurityManager.checkPermission(Unknown Source)
  at java.lang.SecurityManager.checkLink(Unknown Source)
  at java.lang.Runtime.loadLibrary0(Unknown Source)
  at java.lang.System.loadLibrary(Unknown Source)
  at app.DLogger.<clinit>(Unknown Source)
  ... 16 more
java.lang.Exception: java.lang.ExceptionInInitializerError
  at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
  at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.plugin.com.DispatchImpl.invoke(Unknown Source)

The key line seems to be "Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission loadLibrary.DLoggerImpl)" which implies a permissions problem. It could be that I'm getting the policy file wrong - or the signing wrong - or stuff like that or it could be that Java is hardwired to not allow those sort of permissions for an Applet because of the security risk.

My question is am I wasting my time? Can it be done & if so, how?

Thanks in anticipation

Mike

Keibosh :

You can definitely accomplish this. I have a working applet in production that does exactly this. Even if your applet is signed, you still need to use the Access Controller to access the dll, you cannot just call "loadlibrary". You can add this to the Java policy file however this is not recommended due to 1. You probably do not have access to the users java configuration. 2. Even if this is for your own company use, managing the policy file is a pain as users will download some JRE and your policy file is either overwritten or ignored.

You best bet is to sign your jar, making sure to wrap your load library code in a privileged block of code like this.

try
{
    AccessController.doPrivileged(new PrivilegedAction()
    {
        public Object run()
        {
            try
            {
                // privileged code goes here, for example:
                System.load("C:/Program Files/.../Mydll.dll");
                return null; // nothing to return
            }
            catch (Exception e)
            {
                System.out.println("Unable to load Mydll");
                return null;
            }
        }
     });
}
catch (Exception e)
{
    System.out.println("Unable to load Mydll");
}

You can Also use System.loadlibrary(mydll.dll) but you have to have the dll folder on the path in windows so the applet can find it.

If you need some source samples for calling the JNI functions let me know I can grab that as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling into a saved java object via JNI from a different thread

From Java

Calling Javascript functions from a Java Applet

From Java

Monitoring memory usage for a C DLL called with Java via JNI?

From Java

signed applet gives AccessControlException: access denied, when calling from javascript

From Java

JNI calling JVM functions/events from native code?

From Java

Calling C++ dll from Java

From Java

Java Applet and dll

From

Calling Golang from Java using JNI on Windows leads to "A dynamic link library (DLL) initialization routine failed"

From Dev

Calling a static void Java method from JNI

From Dev

Call fortran dll from java using JNI

From Dev

Calling dll implemented JNI from C++

From Dev

Calling functions from Rust DLL with Excel VBA

From Dev

Error calling a function from unmanaged dll

From Dev

Calling C++ dll from python

From Dev

Calling function from dll with pointer not working

From Dev

Calling any function from dll throws error

From Dev

JNI calling Java from C++ with multiple threads

From Dev

Qt Application Calling Dll from Fortran

From Dev

Calling C system calls from JNI

From Dev

FORTRAN DLL calling convention from MapleSim / Modelica

From Dev

Deploying JRE via JNI: jvm.dll not designed to run on Windows or [......]

From Dev

Calling function from dll with QT

From Dev

Calling DLL from another namespace

From Dev

Calling Java Methods from JNI results in program crash

From Dev

Build a dll in C++ for java which is calling another dll (JNI on Eclipse)

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

Calling RenderScript from C / JNI

From Dev

Data type conversion when (JNI) calling a function from cpp lib

From Dev

Calling function from Fortran DLL with Char as parameter

Related Related

  1. 1

    Calling into a saved java object via JNI from a different thread

  2. 2

    Calling Javascript functions from a Java Applet

  3. 3

    Monitoring memory usage for a C DLL called with Java via JNI?

  4. 4

    signed applet gives AccessControlException: access denied, when calling from javascript

  5. 5

    JNI calling JVM functions/events from native code?

  6. 6

    Calling C++ dll from Java

  7. 7

    Java Applet and dll

  8. 8

    Calling Golang from Java using JNI on Windows leads to "A dynamic link library (DLL) initialization routine failed"

  9. 9

    Calling a static void Java method from JNI

  10. 10

    Call fortran dll from java using JNI

  11. 11

    Calling dll implemented JNI from C++

  12. 12

    Calling functions from Rust DLL with Excel VBA

  13. 13

    Error calling a function from unmanaged dll

  14. 14

    Calling C++ dll from python

  15. 15

    Calling function from dll with pointer not working

  16. 16

    Calling any function from dll throws error

  17. 17

    JNI calling Java from C++ with multiple threads

  18. 18

    Qt Application Calling Dll from Fortran

  19. 19

    Calling C system calls from JNI

  20. 20

    FORTRAN DLL calling convention from MapleSim / Modelica

  21. 21

    Deploying JRE via JNI: jvm.dll not designed to run on Windows or [......]

  22. 22

    Calling function from dll with QT

  23. 23

    Calling DLL from another namespace

  24. 24

    Calling Java Methods from JNI results in program crash

  25. 25

    Build a dll in C++ for java which is calling another dll (JNI on Eclipse)

  26. 26

    JNI: Calling a java method from C periodically is not working

  27. 27

    Calling RenderScript from C / JNI

  28. 28

    Data type conversion when (JNI) calling a function from cpp lib

  29. 29

    Calling function from Fortran DLL with Char as parameter

HotTag

Archive