Calling a DLL from an Applet via JNI

后端 未结 3 596
抹茶落季
抹茶落季 2020-12-01 13:16

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 a

相关标签:
3条回答
  • 2020-12-01 13:46

    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.

    0 讨论(0)
  • 2020-12-01 13:49

    The only thing I can suggest is taking a look at the source code for that area and trying to decipher if it is not allowing because of lack of permission or because that is not allowed at all. You don't have line numbers unfortunately, so that makes it a little more tricky.

    0 讨论(0)
  • 2020-12-01 13:59

    I am pretty sure you cannot load a native library from an Applet unless it is "signed", and then the user will get an acceptance dialog to allow or disallow. That is, assuming you can do JNI at all in an applet... never tried that.

    Good luck.

    0 讨论(0)
提交回复
热议问题