Read a file within a jar from java code present in same jar

人走茶凉 提交于 2019-12-29 09:11:06

问题


I am trying to read a xsd file kept in a jar using java code kept inside the same jar. I am using the following code.

URL tmpurl = ClassLoader.getSystemResource("com/abc/filename.xsd");

Schema s = schemaFactory.newSchema(tmpurl);
jaxbUnMarshaller.setSchema(s);

It is working fine when I run it as a separate project but when I make a jar, tmpurl is null, hence the setSchema gives a null pointer exception.

Can you please a workaround that can make it run even inside a jar file.


回答1:


Hava you tried?

getClass().getClassLoader().getResource()

Also your classpath in manifest file in jar does matter.

Please, take a look at accepted answers in here:

Class.getResource and ClassLoader.getSystemResource: is there a reason to prefer one to another?

Loading files with ClassLoader



来源:https://stackoverflow.com/questions/14209107/read-a-file-within-a-jar-from-java-code-present-in-same-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!