Java 如何从jar文件访问资源文件
我读过很多类似的文章,但我找不到解决问题的方法:我需要验证和解析xml文件。(根据xsd模式进行验证)。问题是,在我的方法中,当我运行:mvn包,然后运行jar文件时,我得到以下错误:Java 如何从jar文件访问资源文件,java,maven,Java,Maven,我读过很多类似的文章,但我找不到解决问题的方法:我需要验证和解析xml文件。(根据xsd模式进行验证)。问题是,在我的方法中,当我运行:mvn包,然后运行jar文件时,我得到以下错误: java.lang.IllegalArgumentException: URI is not hierarchical at java.io.File.<init>(Unknown Source) at com.test.Main.main(Main.java:15) 主类 publ
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at com.test.Main.main(Main.java:15)
主类
public class Main {
public static void main(String args[]){
try {
InputStream inXML = Main.class.getResourceAsStream("/xml/carParts.xml");
InputStream inXSD = Main.class.getResourceAsStream("/xml/carPartsWarehouse.xsd");
//1. dom parsing method
System.out.println("**********************DOM PARSING METHOD************************");
DOMParserFile dom = new DOMParserFile(inXML,inXSD);
dom.readFromXMLFile();
dom.printCarParts();
//2. sax parsing method
System.out.println("***********************SAX PARSING METHOD*************************");
SAXParserFile sax = new SAXParserFile(inXML,inXSD);
sax.readDataFromXml();
sax.printCarParts();
//3. stax parsing method
System.out.println("***********************StAX PARSING METHOD*************************");
StAXParserFile stax = new StAXParserFile(inXML,inXSD);
stax.readFromXml();
stax.printCarParts();
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这些不是真正的
文件,它们是嵌入式资源
您可以手动打开它们,因为.jar
是.zip
文件,其中资源是文件。但这不是一种优雅的处理方式
不过,您可以按要求打开。因此:
DOMParserFile
在哪里?@CommuSoft我按照您的建议使用InputStream修改了代码,现在我在将同一个流作为DOMParser、SAXParser和StAXParser的参数传递时遇到了问题。我得到java.io.IOException:streamclosed
。你能帮我吗?你能修改答案并提供完整的源代码吗?@CommuSoft我编辑了这个问题。好的,一个流从头到尾都在读取信息,所以在第一个解析器读取信息后,光标位于流的末尾。因此,您应该在两次读取之间调用inXML.reset()
和inXSD.reset()
,或者构造一个新的InputStream
。
public class XMLFileValidator {
public static boolean validateAgainstXSD(InputStream xmlFile, InputStream xsdFile)
{
try
{
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(xsdFile));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xmlFile));
System.out.println("The XML file IS valid");
return true;
}
catch(Exception ex)
{
System.out.println("The XML file is NOT valid");
System.out.println("Reason: " + ex.getLocalizedMessage());
ex.printStackTrace();
return false;
}
}
}
public class Main {
public static void main(String args[]){
try {
InputStream inXML = Main.class.getResourceAsStream("/xml/carParts.xml");
InputStream inXSD = Main.class.getResourceAsStream("/xml/carPartsWarehouse.xsd");
//1. dom parsing method
System.out.println("**********************DOM PARSING METHOD************************");
DOMParserFile dom = new DOMParserFile(inXML,inXSD);
dom.readFromXMLFile();
dom.printCarParts();
//2. sax parsing method
System.out.println("***********************SAX PARSING METHOD*************************");
SAXParserFile sax = new SAXParserFile(inXML,inXSD);
sax.readDataFromXml();
sax.printCarParts();
//3. stax parsing method
System.out.println("***********************StAX PARSING METHOD*************************");
StAXParserFile stax = new StAXParserFile(inXML,inXSD);
stax.readFromXml();
stax.printCarParts();
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
InputStream s = Main.class.getResourceAsStream("/xml/carParts.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(s);
//process document