private ConfigurableEmitter emitter;
File xmlFile = new File("ressources/emitter.xml");
emitter = ParticleIO.loadEmitter(xmlFile);
如果我在 Eclipse 中启动项目,一切都会正常工作,但是在导出项目并使用 JarSplice 创建 .jar 文件后,当我使用命令提示符启动 jar 文件时,程序将崩溃并启动 FileNotFoundException ,说找不到指定的路径。
java.io.FileNotFoundException: ressources\emitter.xml (The system cannot find the
path specified)
令人惊讶的是,就在打开 xml 文件之前,我打开了一个与 xml 文件位于同一位置的 .png 文件,这没有任何问题。另外,当我打开使用winrar导出的.jar文件时,我可以在ressources文件夹下找到我的xml文件。这里可能出现什么问题?
谢谢!
编辑:
代码及解决方案:
InputStream i=this.getClass().getClassLoader().
getResourceAsStream("ressources/test.xml");
emitter = ParticleIO.loadEmitter(i);
最佳答案
当您将项目打包到 JAR 时,您的资源并不存在于磁盘上,而是被压缩到 JAR 本身中,并且您必须作为资源加载。
有很多关于如何加载的指南使用 ClassLoader.getResourceAsStrem()
从 JAR 获取资源(按照此 link )
关于java - 在java中使用slick2D库的xml FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18093092/