未捕获错误:在javascript中调用NPObject上的方法时出错

时间:2012-04-28 10:37:24

标签: java javascript html applet

我想使用applet来更改存储库。为此,我编写了以下代码:

LoadOntology:

package owlapi.loader;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.*;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.util.OWLEntityRenamer;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;


public class LoadOntology {
    private static String             ontologyDir   = "./owlapi/loader/featurepool.owl";
    private static OWLOntology        localOntology;
    private static OWLOntologyManager manager;
    private static OWLDataFactory     factory;
    private static PrefixManager      pm            = new DefaultPrefixManager("http://wise.vub.ac.be/Members/lamia/variability/Feature_Assembly/FAM.owl#");

public LoadOntology() {
    manager = OWLManager.createOWLOntologyManager();
    File file = new File(ontologyDir);
    try {
        localOntology = manager.loadOntologyFromOntologyDocument(file);
        System.out.println("Loaded ontology: " + localOntology);
        factory = manager.getOWLDataFactory();
        }   
    catch (UnparsableOntologyException e) {
        System.out.println("Could not parse the ontology: " + e.getMessage());
        Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
        for (OWLParser parser : exceptions.keySet()) {
            System.out.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
            System.out.println("Failed because: " + exceptions.get(parser).getMessage());
        }
    }
    catch (UnloadableImportException e) {
        System.out.println("Could not load import: " + e.getImportsDeclaration());
        OWLOntologyCreationException cause = e.getOntologyCreationException();
        System.out.println("Reason: " + cause.getMessage());
    }
    catch (OWLOntologyCreationException e) {
        System.out.println("Could not load ontology: " + e.getMessage());
    }   
}

public void addInstance(String Class, String individual) {
    OWLClass owlClass = factory.getOWLClass(Class, pm);
    OWLClass superClass = factory.getOWLThing();
    OWLIndividual instance = factory.getOWLNamedIndividual(individual, pm);

    OWLClassAssertionAxiom classAssertion1 = factory.getOWLClassAssertionAxiom(superClass, instance);
    manager.addAxiom(localOntology, classAssertion1);
    OWLClassAssertionAxiom classAssertion2 = factory.getOWLClassAssertionAxiom(owlClass, instance);
    manager.addAxiom(localOntology, classAssertion2);
    System.out.println("Instance added");
}
}

InJava(用javascript加载的applet):

public class InJava extends Applet{
private static LoadOntology loadedOntology;

public void addInstance(String Class, String individual) {
    Graphics g = getGraphics();
     g.drawString("Test", 10, 10);
    loadedOntology = new LoadOntology();
    loadedOntology.addInstance(Class, individual);
    }
}

当我加载applet时,根本没有错误,但是当我尝试调用addInstance方法时:

<input type="button" value="call Java Applet method" onClick = 'document.owlapi.addInstance("Abstract_Feature", "New_Test")'>
<APPLET CODE="owlapi/InJava.class" NAME="owlapi" HEIGHT=100 WIDTH=100> </APPLET>  

浏览器抛出错误Uncaught Error: Error calling method on NPObject.在stackoverflow上阅读了一些关于此问题的问题之后,我得出的结论是该插件必须有问题。

我在方法中添加了一些测试代码(drawString(“Test”,10,10)),所以我建议LoadOntology类的构造会产生一些问题(这个类在作品中分别进行了测试)在其自己的)。有人看到我做错了吗?

update:我添加了applet使用的java类的一部分:repository loader。所有提供的代码都应该编译。

更新:我没有使用MASCRIPT/SCRIPTABLE因为代码基于this示例,该示例适用于我当前的浏览器(chrome)

udpate:您必须将owlapi jar包含在buildpath中,可以下载here

1 个答案:

答案 0 :(得分:1)

根据我所知道的片段,我建议尝试使用此HTML。

<html>
<body>
<APPLET 
    CODE="owlapi.InJava" 
    NAME="owlapi" 
    archive="owlapi/owl1.jar,owlapi/owl2.jar"
    HEIGHT=100 
    WIDTH=100> 
</APPLET>  
</body>
</html>

这个HTML“静态地”嵌入applet - 不使用JavaScript。类名(code属性)应该是类的完全限定名,而不是文件名。虽然后者通常是可以容忍的,但这是不正确的。它显示了如何从owlapi目录导入API,根据需要更改名称。

同样值得关注的是混合松散的类文件和Jars。这往往会混淆开发人员和JRE。如果将owlapi.InJava类放在my.jar内,并且该Jar与其余的放在同一目录中,则可以添加codebase并缩短archive路径。像这样:

<html>
<body>
<APPLET 
    CODE="owlapi.InJava" 
    NAME="owlapi" 
    codebase="owlapi"
    archive="my.jar,owl1.jar,owl2.jar"
    HEIGHT=100 
    WIDTH=100> 
</APPLET>  
</body>
</html>

这不是有效的HTML。这只是一个检查基础知识的测试。稍后,您需要使用有效的HTML并(可能)使用applet info. page中提到的deployJava.js部署applet。