简体   繁体   中英

Can Xerces Java Parser load Resource from ClassPath and run it with Jar executable?

I want to validate xml file against xs schema file. Now, I'm providing xml file as a command line arg. and need to keep my schema file inside my classpath.

For this I've used Xerces (2_11_0) parser for java

I've kept schema file inside package and added this folder inside my classpath.

String SCHEMA = this.getClass().getResource(SCHEMA_NAME).getFile();
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", SCHEMA);

When I run the code inside eclipse, it works like charm but the problem arises when I export my project as an executable jar.

I try running the jar and it is not able to find SCHEMA file.

file:/C:/ MY_JAR! /PACKAGE/SCHEMA_FILE.xsd

Can anyone let me know the reason why this is not working from executable jar and the solution for it?

Let me know if more info is required.

Thanks

That's because the URL for this.getClass().getResource(SCHEMA_NAME) is now something like jar:file:/C:/MY_JAR!/PACKAGE/SCHEMA_FILE.xsd . See if just using this.getClass().getResource(SCHEMA_NAME).toString() will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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