繁体   English   中英

在jar文件中查找类路径文件时出错

[英]error finding class path file in jar file

运行Java应用程序时出现gettine错误。 我的应用程序捆绑在一个.jar文件中。 这意味着xsd文件位于.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;
}

尝试将通话更改为

new File("...")

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

甚至

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

打包应用程序时,需要让类加载器为您获取文件。

不应该是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM