简体   繁体   中英

Run Priviliged Code in a Java Applet

I am calling a Java applet from Javascript. The Java code needs to run in privileged mode (eventually it is going to display a file chooser which will display files from the local hard disk). The Javascript call returns java.lang.reflect.InvocationTargetException . Any ideas on how I can get this code to work, or any debugging techniques to get more information as to why it is failing would be greatly appreciated.

Here's the Java class. I've stripped out all unnecessary code to try to reveal the actual problem.

// Java code

public class OHLib extends Applet {

    public String getFile() { 

        String result;
        try {
            // this code does NOT generate an exception
            result = (String) AccessController.doPrivileged(new PrivilegedAction() {
                public String run() {
                    // JFileChooser code will go here
                    return "xxx";
               }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }
}

I'm not getting any exception information from the above try/catch block.

The above code is called by JavaScript, as follows:

// JavaScript code 

function BrowseForFile() {

    var x;
    try {
         // this code generates InvocationTargetException
         x = ohApplet.getFile();
    } catch (e) {
         console.log(e);
    }

}

The above code is where I'm getting the InvocationTargetException .

The applet is deployed on my web page as follows:

<script src="/plugins/deployJava.js"></script>

<script>
    var attributes = { 
        id:'ohApplet',
        code:'OHLib',
        codebase: 'java',
        archive: 'OHLib.jar',   
        width:1, 
        height:1,
    } ;
    var parameters = { 
        jnlp_href: 'OHLib.jnlp',
        classloader_cache: 'false',
    } ;
    deployJava.runApplet(attributes, parameters, '1.6');

The applet is in a signed jar file, with the following manifest:

Application-Name: <appname>
Permissions: all-permissions
Codebase: <domain>.dev <domain>.com
Caller-Allowable-Codebase: <domain>.dev <domain>.com
Application-Library-Allowable-Codebase: <domain>.dev <domain>.com

The JNLP file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">

    <information>
        <title>...</title>
        <vendor>...</vendor>
    </information>

    <security>
        <all-permissions />
    </security>

    <resources>
        <j2se version="1.8+" href="http://java.sun.com/products/autodl/j2se" />
        <jar href="OHLib.jar" main="true" />
    </resources>

    <applet-desc 
         main-class="OHLib"
         name="OHLib"
         width="1"
         height="1">
     </applet-desc>
</jnlp>         

Any ideas on how I can get this code to work, or any debugging techniques to get more information as to why it is failing would be greatly appreciated.

The problem was that I was not including the anonymous inner class (OHLib$1.class) in the jar file. My original jar command looked like this:

jar cfmv OHLib.jar "../../jar_manifest.txt" OHLib.class

Changing it to below fixed the problem:

jar cfmv OHLib.jar "../../jar_manifest.txt" OHLib.class OHLib$1.class

Credits go to this page for the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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