使用java中的slick2D库的xml FileNotFoundException [英] xml FileNotFoundException using slick2D library in java
问题描述
private ConfigurableEmitter emitter;
File xmlFile = new File("ressources/emitter.xml");
emitter = ParticleIO.loadEmitter(xmlFile);
如果我在eclipse中启动项目,一切都会正常工作,但在我导出项目后使用JarSplice创建一个.jar文件,当我使用命令提示符启动jar文件时,程序将崩溃启动FileNotFoundException,说它无法找到指定的路径。
If I launch the project in eclipse, everything will works fine, but after I export my project and use JarSplice to create a .jar file, when I launch the jar file using the command prompt, the program will crash launching a FileNotFoundException, saying it cannot find the path specified.
java.io.FileNotFoundException: ressources\emitter.xml (The system cannot find the
path specified)
令人惊讶的是,在打开xml文件之前,我打开一个与xml文件位于同一位置的.png文件,这没有任何问题。另外,当我打开使用winrar导出的.jar文件时,我可以在ressources文件夹下找到我的xml文件。这可能是什么问题?
The surprising thing is that just before opening the xml file, I open a .png file located at the same place as the xml file, and this without any problem. In addition, when I open the .jar file I exported using winrar, I can find my xml file under the ressources folder. What can be the problem here?
谢谢!
编辑:
带解决方案的代码:
InputStream i=this.getClass().getClassLoader().
getResourceAsStream("ressources/test.xml");
emitter = ParticleIO.loadEmitter(i);
推荐答案
当您将项目打包到JAR时,您的资源不会t存在于磁盘上,但是被压缩到JAR本身并且你必须作为资源加载。
关于如何使用 ClassLoader.getResourceAsStrem从JAR加载资源,有很多关于SO的指南。 )
(按照链接)
When you pack your project to JAR your resource don't live on disk, but are compressed into the JAR itself and you have to load as resource.
There are a lot of guide on SO on how to load resource from JAR using ClassLoader.getResourceAsStrem()
(follow this link)
这篇关于使用java中的slick2D库的xml FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!