【发布时间】:2014-01-18 03:21:15
【问题描述】:
我正在尝试在下面的类中读取 xml 配置。它在运行时正在工作。但是当我将它导出到可运行的 Jar 中时。文件没有被读取..??
public KeyTranslator() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String sPath = "res/config.xml";
//InputStream is = (InputStream) Thread.currentThread().getContextClassLoader().getResourceAsStream(sPath);
//InputStream is = this.getClass().getResourceAsStream((sPath);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
if (AppFrame.jar == null) {
this.myDoc = builder.parse( new File((AppFrame.basePath+sPath).replace('\\', '/')) );//ensure seperator is '/' as linux is picky
} else {
this.myDoc = builder.parse(AppFrame.jar.getInputStream(AppFrame.jar.getJarEntry(sPath)));
}
}
我用谷歌搜索并找到了 getResourceAsStream 方法。但它似乎抛出 FileNotFoundException。& 我不知道如何在我的代码中添加 InputSream ???
所以请帮助我正确的方向:)
感谢您的帮助...
注意 我试过的方法被评论了
【问题讨论】:
-
你把jar添加到类路径了吗?
-
看看this,对你有帮助。
标签: java xml parsing jframe executable-jar