2013-01-03 79 views
1

我使用NetBeans和我寫了這個功能,與其他線程的幫助下把同樣的問題, ,但我得到線「InputStream is = getClass().getResourceAsStream(xml_file_path);」 錯誤說:「non-static method getClass() cannot be referenced from a static context獲取XML文件

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,但沒有成功。

+0

大量重複的:http://stackoverflow.com/search?q=getResourceAsStream – parsifal

回答

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

您必須使用:

YourClass.class.getResourceAsStream() 

,而不是getClass()

0

試試這個:

InputStream is = YourClass.class.getClassLoader().getResourceAsStream(xml_file_path);