从通过JNI一个Applet调用DLL [英] Calling a DLL from an Applet via JNI

查看:255
本文介绍了从通过JNI一个Applet调用DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一件工作是跨越到一些陌生的领域概念证明。
我任务是对我们的内联网在浏览器中的EFTPOS机连接到一个应用程序的运行作为applet。

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.

我忽视了当下EFTPOS DLL并创建了一个简单的JNI的DLL装饰在我选择的语言(德尔福),用来记录一个字符串在C文本文件:\\,我可以从本地成功地叫它Java应用程序。

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.

然而,当我创建一个小程序做同样的事情,它编译成一个.jar,签署JAR和放大器;尝试通过JavaScript来调用applet的方法网页失败的。

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.

一个高级Java的家伙我的工作不认为这将有可能得到这个工作,因为它是天生的邪恶,让一个applet做到这一点。

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.

有就是你可以把一个java.policy文件允许调用LoadLibrary一个条目。还有的AllPermission&放大器;我试着沿着这些线路变化的整个主机都无济于事生产Java控制台下面的错误跟踪:

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)

重点线似乎是产生的原因:java.security.AccessControlException:访问被拒绝(java.lang.RuntimePermission loadLibrary.DLoggerImpl),这意味着权限问题。这可能是因为我得到了政策文件错误 - 或签约错误 - 或者类似的东西,也可能是因为Java是硬连线不允许这些排序的权限,因为安全风险的一个Applet

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?

谢谢期待

迈克

推荐答案

您绝对可以做到这一点。我在那生产不正是这样一个工作的小程序。即使你的小应用程序进行签名,你仍然需要使用访问控制器访问的dll,你不能仅仅称之为调用LoadLibrary。您可以将它添加到然而,这是不推荐的Java策略文件,因为1.您可能没有访问用户Java配置。 2.即使这是你自己的公司使用,管理的政策文件是一个痛苦,因为用户会下载一些JRE和策略文件要么是覆盖或忽略。

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.

您最好的选择是签署您的罐子,并确保您的包裹装载库code在code这样的特权块。

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");
}

您还可以使用的System.loadLibrary(MYDLL.DLL),但你必须有路径上的DLL文件夹窗口,使小程序可以找到它。

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.

如果您需要调用JNI功能的一些源样本让我知道我可以抓住这一点。

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

这篇关于从通过JNI一个Applet调用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆