2014-02-26 70 views
0

運行我的java應用程序時出現gettine錯誤。我的應用程序捆綁在一個.jar文件中。這意味着xsd文件位於.jar文件中。在jar文件中發現類路徑文件時出錯

錯誤:錯誤的

class path resource [classpath*:myschema.xsd] cannot be opened because it does not exist 

來源:

@Value("classpath*:myschema.xsd") 
private Resource xsdFile; 

public boolean validateXML()() throws IOException { 
    boolean isValidXMLFile = false; 

    Source xmlFile = new StreamSource(new File(xmlDataFile)); 
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    Schema schema = null; 
    File outXsdFile = new File("autogenpath.xsd"); 
    InputStream is =xsdFile.getInputStream(); 
    OutputStream out=new FileOutputStream(outXsdFile); 
    IOUtils.copy(is, out); 

    try { 
     schema = schemaFactory.newSchema(outXsdFile); 
    } catch (SAXException e1) { 
     e1.printStackTrace(); 
     isValidXMLFile = false; 
     log.debug("Error reading schema " + e1.getMessage()); 
    } 
    try { 
     Validator validator = schema.newValidator(); 
     validator.validate(xmlFile); 
     log.debug(xmlFile.getSystemId() + " is valid"); 
     isValidXMLFile = true; 

    } catch (SAXParseException e) { 
     isValidXMLFile = false; 
     log.debug(xmlFile.getSystemId() + " is NOT valid"); 
     log.debug("Reason\t\t: " + e.getLocalizedMessage()); 
     log.debug("Line Number \t: " + e.getLineNumber()); 
     log.debug("Column Number\t: " + e.getColumnNumber()); 
     log.debug("Public Id\t: " + e.getPublicId()); 
    } catch (SAXException e) { 
     isValidXMLFile = false; 
     log.debug(xmlFile.getSystemId() + " is NOT valid"); 
     log.debug("Reason\t: " + e.getLocalizedMessage()); 
    } 
    finally{ 
     try{ 
     out.close(); 
     }catch(Exception e){ 
      e.printStackTrace(); 

     } 
    } 
    return isValidXMLFile; 
} 

回答

0

嘗試改變調用

new File("...") 

getClass().getClassLoader().getResource("...") 

甚至

getClass().getClassLoader().getResourceStream("...") 

當你罐子您的應用程序,你需要問的類加載器來獲取文件給你。

+0

我用的getResourceAsStream()他是爲我工作。謝謝 – mahesh

0

它不應該是:

@Value("${classpath}/myschema.xsd") 
private Resource xsdFile; 
+0

感謝您的回覆,我不知道爲什麼我的類路徑資源顯示不同的東西,但它有xsd文件。但仍然無法加載 – mahesh

相關問題