jar中找不到文件异常 [英] file not found exception in jar
本文介绍了jar中找不到文件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class ABC {
public ABC() {
File file = new File("xyz.xml");
但是当我运行我的jar时如下:
but when I run my jar as follows:
java -jar filename.jar arguments....
然后它显示错误:
java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:120)
at com.ensarm.niidle.web.proxy.ABC.<init>(ABC.java:47)
如何解决?
推荐答案
如果需要读取JAR中的文件内容,则不能直接使用File类。
使用ClassLoader加载它:
If you need to read file content in JARs, you can not use File class directly. Using ClassLoader to load it:
//例如读取默认包中的SeleniumConfiguration.xml
// for example read the SeleniumConfiguration.xml in the default package
InputStream input = SeleniumConfiguration.class.getResourceAsStream("/SeleniumConfiguration.xml");
这篇关于jar中找不到文件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文