小程序.java.lang.reflect.InvocationTargetException [英] applet. java.lang.reflect.InvocationTargetException

查看:29
本文介绍了小程序.java.lang.reflect.InvocationTargetException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 jna Pointer 类的小程序.小程序代码为:

I have applet which use jna Pointer class. 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();
        }
    }
}

在 html 代码中我这样声明小程序:

In html code I declared applet this way:

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

当我通过 javascript 调用 document.getElementById("Applet1").test() 时,会出现 java.lang.reflect.InvocationTargetException.我不能在 java 类端调用 e.getCause(),因为小程序 try/catch 没有捕获错误(我不明白为什么).但是 javascript try/catch 会捕获此错误.如果移动 Pointer p = new Memory(73); 行就可以了.问题是这条线.请帮助解决问题.

When i call document.getElementById("Applet1").test() by javascript the java.lang.reflect.InvocationTargetException arise. I cant call e.getCause() in the java class side, because applet try/catch dont catch the error (I dont understand why). But javascript try/catch catch this error. If move Pointer p = new Memory(73); line it will be ok. The matter is this line. Please, help to fix the problem.

如果替换此块:

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

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

我收到 java.security.AccessControlException: access denied (java.util.PropertyPermission jna.boot.library.path read)

I got java.security.AccessControlException: access denied (java.util.PropertyPermission jna.boot.library.path read)

推荐答案

好的,现在我们来到了问题的根源.(您仍然可以使用 printStackTrace - 这也应该打印 cause 的堆栈跟踪.

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.).

  1. 未签名的小程序只能访问有限数量的系统属性 - jna 属性不是这些的一部分.

  1. Unsigned applets have only access to a limited number of system properties - the jna properties are not part of these.

在未签名的小程序中,您无论如何都无法加载本机库,因此无法使用 JNA(顺便说一下,或 JNI).

In an unsigned applet you can't load native libraries anyways, so no way to use JNA (or JNI, by the way).

如果您对小程序进行签名(并告诉插件接受签名),则您的小程序具有使用 JNA 的必要权限.但任何单个运行代码的权限实际上是调用当前代码的所有方法的权限的交集.

If you sign the applet (and tell the plugin to accept the signature), your applet has the necessary rights to use JNA. But the rights of any single running code is effectively the intersection of the rights of all methods which called the current code.

从 JavaScript 调用的 Applet 方法的权限极其有限(因为插件无法真正检查 JavaScript 代码是否具有必要的权限,如果您的浏览器甚至有这样的概念).

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).

您可以通过使用 AccessController.doPrivileged(...) 包装需要在您的小程序的权限下运行的代码部分来解决此问题.但是首先确保这不会做任何危险的事情(这对于 JNI/JNA 来说很容易),即使是从恶意 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(...). But first make sure this can't do anything dangerous (which is easy with JNI/JNA), even when called from malicious JavaScript code.

这篇关于小程序.java.lang.reflect.InvocationTargetException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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