0

I start a java applet on my website with deployJava.js from Oracle.

    var attributes = {  id:'applet', 
                    code:'dummy.Applet.class',
                    archive: contextPath + '/jar/JAR_NAME.jar',
                    width:1, 
                    height:1
                } ;
var parameters = {jnlp_href: 'Applet.jnlp',
                        baseUrlParam: baseUrl,
                        serverConnectionType: 'REST',
                        java_status_events: 'true'
                } ;
deployJava.runApplet(attributes, parameters, '1.6');

The problem is that if I call a java function from the javascript and the applet throw a RuntimeException, I can not catch this in a try-catch from Javascript. I catch everytime only the "Uncaught Error: Error calling method on NPObject." Error.

For example:

I call this function in javascript:

function getConfirmation(){    
    applet.confirmation()
}

Then the applet throw a ApplicationBaseException without a try-catch block I can see two errors:

Uncaught Error: dummy.ApplicationBaseException         toperson.js:78
                getConfirmation                        toperson.js:78
                (anonymous function)                   toperson.js:53
                b.event.dispatch                jquery-1.9.1.min.js:3
                v.handle                        jquery-1.9.1.min.js:3

Uncaught Error: Error calling method on NPObject.
                getConfirmation
                (anonymous function)                   toperson.js:53
                b.event.dispatch                jquery-1.9.1.min.js:3
                v.handle                        jquery-1.9.1.min.js:3

but with a try-catch block I only can catch the NPObject error.

How can I catch the other error in Java-Script?

4

1 回答 1

1

We solve this issue by using a small wrapper:

public class JSWrapper {

    private Object returnVal;
    private Exception exception;

    public void setRV(Object returnVal) {
        this.returnVal = returnVal;
    }

    public Object getRV() {
        return this.returnVal;
    }

    public void setException(Exception exception) {
        this.exception = exception;             
    }

    public Exception getException() {
        return this.exception;              
    }

}

A method, which is called by javascript should look then like this:

public JSWrapper selectFolder(){
    final Applet applet = this;
    return AccessController.doPrivileged(new PrivilegedAction<JSWrapper>() {
        public JSWrapper run() {
            JSWrapper wrap = new JSWrapper();
            try{        
                JFileChooser folderchooser = new JFileChooser();
                int returnValue = folderchooser.showSaveDialog(applet);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    wrap.setRV(folderchooser.getSelectedFile().getAbsolutePath());
                } else {
                    wrap.setRV(null);
                }
            } catch (ApplicationBaseException e) {
                wrap.setRV(null);
                wrap.setException(e);                   
            } catch (Exception e2) {
                wrap.setRV(null);
                wrap.setException(e2);  
            }               
            return wrap;
        }
    });                     
}

In javascript you can check the return code or catch the exception and throw it:

var folderResult = applet.selectFolder();
if (folderResult.getException()) {
    throw new Exception(folderResult.getException());
}       
if(folderResult.getRV()){
    alert("Folder selected:" + folderResult.getRV());
}
于 2013-09-17T07:12:41.647 回答