1

我正在使用 NetBeans,我在其他线程的帮助下编写了这个函数,但我得到了行错误“ InputStream is = getClass().getResourceAsStream(xml_file_path);”说:“ non-static method getClass() cannot be referenced from a static context

public static Document Get_XML_Document_From_Jar(String xml_file_path) {
    Document xml_doc = null;

    InputStream is = getClass().getResourceAsStream(xml_file_path);
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        xml_doc = db.parse(is);                          // just use a different parse method   
        xml_doc.getDocumentElement().normalize();
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    return xml_doc;
}

我能做些什么?我尝试使用 ClassLoader 但没有成功。

4

2 回答 2

4
non-static method getClass() cannot be referenced from a static context

您必须使用:

YourClass.class.getResourceAsStream()

而不是getClass().

于 2013-01-03T16:08:44.217 回答
0

尝试这个:

InputStream is = YourClass.class.getClassLoader().getResourceAsStream(xml_file_path);
于 2013-01-03T16:10:53.180 回答