简体   繁体   English

小程序。 java.lang.reflect.InvocationTargetException

[英]applet. java.lang.reflect.InvocationTargetException

I have applet which use jna Pointer class. 我有applet使用jna Pointer类。 The applet code is: 小程序代码是:

import com.sun.jna.*;
public class Applet1 extends Applet{
    public void test() {
        try {
            Pointer p = new Memory(73);
        } catch (Exception e) {
        e.printStackTrace();
        }
    }
}

In html code I declared applet this way: 在html代码中,我以这种方式声明了applet:

<applet
    codebase=/pki/
    code=Applet1.class 
    archive=/pki/jna-3.2.3.jar
    id=Applet1
    width=100 
    height=100 >
</applet>

When i call document.getElementById("Applet1").test() by javascript the java.lang.reflect.InvocationTargetException arise. 当我通过javascript调用document.getElementById(“Applet1”)。test()时,会出现java.lang.reflect.InvocationTargetException。 I cant call e.getCause() in the java class side, because applet try/catch dont catch the error (I dont understand why). 我不能在java类中调用e.getCause(),因为applet try / catch不会捕获错误(我不明白为什么)。 But javascript try/catch catch this error. 但javascript try / catch捕获此错误。 If move Pointer p = new Memory(73); 如果移动Pointer p = new Memory(73); line it will be ok. 它会好的。 The matter is this line. 问题是这一行。 Please, help to fix the problem. 请帮助解决问题。

EDIT: if replace this block: 编辑:如果替换此块:

try {
    Pointer p = new Memory(73);
} catch (Exception e) {
    e.printStackTrace();
}

to

try {
    Pointer p = new Memory(73);
} catch (Throwable e) {
    System.out.println(e.getCause());
}

I got java.security.AccessControlException: access denied (java.util.PropertyPermission jna.boot.library.path read) 我得到java.security.AccessControlException:访问被拒绝(java.util.PropertyPermission jna.boot.library.path read)

Okay, now we come to the root of the problem. 好的,现在我们来看问题的根源。 (You still could have used printStackTrace - this should have printed the stack trace of the cause , too.). (您仍然可以使用printStackTrace - 这也应该打印了cause的堆栈跟踪。)。

  1. Unsigned applets have only access to a limited number of system properties - the jna properties are not part of these. 未签名的小程序只能访问有限数量的系统属性 - jna属性不属于这些属性。

  2. In an unsigned applet you can't load native libraries anyways, so no way to use JNA (or JNI, by the way). 在未签名的applet中,无论如何都无法加载本机库,因此无法使用JNA(或JNI,顺便说一下)。

  3. If you sign the applet (and tell the plugin to accept the signature), your applet has the necessary rights to use JNA. 如果您对applet进行签名(并告诉插件接受签名),则您的applet具有使用JNA的必要权限。 But the rights of any single running code is effectively the intersection of the rights of all methods which called the current code. 但是,任何单个运行代码的权利实际上是所有称为当前代码的方法的权利的交集。

    Applet methods called from JavaScript have extremely limited permissions (since the Plugin can't really check that the JavaScript code has the necessary permissions, if your browser even has such a concept). 从JavaScript调用的Applet方法具有极其有限的权限(因为如果您的浏览器甚至具有这样的概念,则插件无法真正检查JavaScript代码是否具有必要的权限)。

    You can go around this by wrapping the part of the code, which needs to run with your applet's permissions, with AccessController.doPrivileged(...) . 您可以通过使用AccessController.doPrivileged(...)包装需要使用您的applet权限运行的代码部分来解决这个问题。 But first make sure this can't do anything dangerous (which is easy with JNI/JNA), even when called from malicious JavaScript code. 但首先要确保这不会做任何危险 (使用JNI / JNA很容易),即使从恶意JavaScript代码调用也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM