Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

xml FileNotFoundException using slick2D library in java

private ConfigurableEmitter emitter;

File xmlFile = new File("ressources/emitter.xml");
emitter = ParticleIO.loadEmitter(xmlFile);

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: ressourcesemitter.xml (The system cannot find the
path specified)

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?

Thank you!

EDIT :

Code with solution:

InputStream i=this.getClass().getClassLoader().
    getResourceAsStream("ressources/test.xml");
emitter = ParticleIO.loadEmitter(i);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...