I’m developing a application where I need to load a schema file (xsd) as resource from the jar file, but the file path of getClass().getResource(“..”) is getting prefixed with the working directory.
This is the code:
//ClassLoader classLoader = getClass().getClassLoader();
URL xsdUrl = getClass().getResource(
"/schema_" + XSD_VERSION.replace(".", "_") + ".xsd"
);
The xsdUrl should be: file:\D:\NetBeansProjects\Java\reports_cli\target\reports_cli-1.0-Beta-jar-with-dependencies.jar!\schema_1_1.xsd
But is returning: C:\Users\joao\file:\D:\NetBeansProjects\Java\reports_cli\target\reports_cli-1.0-Beta-jar-with-dependencies.jar!\schema_1_1.xsd
Is prefixing C:\Users\joao\ to the file path
This is the project: https://github.com/joaomfrebelo/reports_core
and this is the file: https://github.com/joaomfrebelo/reports_core/blob/master/src/main/java/rebelo/reports/core/parse/AParse.java
All method code:
public Unmarshaller unmarshallInstance() throws SAXException, JAXBException {
LOG.trace(() -> "Create unmarshallInstance");
LOG.trace(() -> "Read xsd file");
//ClassLoader classLoader = getClass().getClassLoader();
URL xsdUrl = getClass().getResource(
"/schema_" + XSD_VERSION.replace(".", "_") + ".xsd"
);
LOG.trace(() -> "Creating JAXBContext.newInstance ");
JAXBContext jaxbContext = JAXBContext.newInstance(Rreport.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
if (xsdUrl == null) {
LOG.warn(() -> "Schema not seted please put the schema file ('schema_"
+ XSD_VERSION.replace(".", "_")
+ ".xsd' in the same folder as your java file");
} else {
File xsd = new File(xsdUrl.getFile());
if (xsd.isFile() && xsd.canRead()) {
LOG.debug(() -> "Seting xsd shema file '" + xsd.getAbsolutePath() + "'");
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(xsd);
unmarshaller.setSchema(schema);
LOG.trace(() -> "Schema seted");
} else {
LOG.debug(() -> "XSD shema path file '"
+ xsd.getAbsolutePath()
+ "' is not a file or is not readable");
}
}
return unmarshaller;
}