2

Yesterday, I had a problem because I couldn't manage to open a xml file (it owuld give me a FileNotFoundException) located in the ressources folder of my .jar file, which I managed to open on eclipse using the following lines of code. You can see my old problem here. This was my code with the problem :

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

Someone told me it that one way was to use getClassLoader().getRessourceAsStream method to open a xml file in a .jar file that was exported

InputStream i= this.getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");
ConfigurableEmitter emitter = ParticleIO.loadEmitter(i);

Unfortunately, that solution only works when I export my project into a .jar file, so if I want to go back debugging my program, I have to take the old code that would only works on eclipse.

My question is: is there any better way to do this without having to change my code if I want to export it or if I want to debug it?

Thank you

edit : Thank you all, it works perfectly fine now my problem was that I put my ressources folder like that :

+project

  +src

  +ressources

    +emitter.xml
Community
  • 1
  • 1
Theriotgames Riot
  • 333
  • 3
  • 6
  • 14

4 Answers4

1

Place the file alongside your source files, then you can use the getResourceAsStream() method in both cases. Don't forget to update the path (which should be the package name of your class, but with slashes instead of dots).

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
1
InputStream i= this.getClass().getClassLoader().getResourceAsStream("/ressources/emitter.xml");

The above should work in both cases (Note is is /resources/.... This is assuming say your directory structure is below:

MyProject

+src
    +ressources
         emitter.xml
Community
  • 1
  • 1
Jatin
  • 31,116
  • 15
  • 98
  • 163
0

My question is: is there any better way to do this without having to change my code if I want to export it or if I want to debug it?

Yes, use Maven. Maven will handle that and it hooks into Eclipse beautifully (NetBeans too!) What you do is place the resource in src/main/resources and then you can have Eclipse run the test goal of the Maven project or you can just run mvn test from the command line. Another advantage of using Maven here is that you can also have src/test/resources/emitter.xml which overrides the one in src/main with environment-specific test instructions and it won't affect your deployment.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0
InputStream i= getClass().getClassLoader().getResourceAsStream("ressources/emitter.xml");

or

InputStream i= getClass().getResourceAsStream("/ressources/emitter.xml");

(note the absolute positioning)

both work when the class is in the same jar, on the same class path.

In the jar the names must be case sensitive, but as the jar already works. Ensure that the ressources directory is on the class path too, or copied to the target directory.

As "ressources" is probably configured yourself (not named "resources" as in English), you probably need to add it to the build somehow.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138