18

I know that there are other questions out there targeting the same issue but the thing is that the solutions don't work for me. I have a little tool that is supposed to read files I want to have and package as resources and relies on other projects (I am using Eclipse Helios) which I'd love to have as jars instead of single classes.

As an application within Eclipse I can access my resources via

String path = MyClass.class.getClassLoader().getResource("resources/" + name + ".SOURCE").getPath();
System.out.println(path);
File file = new File(MyClass.class.getClassLoader().getResource("resources/" + name + ".SOURCE").toURI());
defaultSource = readSOURCEFile(file);

if and only if I place the resources folder under the output path (the compiled sources) NOT if I place it in my src folder.

When I package the project, resources in the root folder or in the classes folder are not packaged at all. If I have the resources in my src folder it get's packaged under src/resources.

I use Export - Runnable Jar, what am I doing wrong? If I try with Export - Jar, I can package my sources and resources properly but I have issues with setting the main class and can't execute the jar. The Manifest is correct and the class is there :-(.

Second problem: In the packaged jar I get the error: URI is not hierarchical (after I moved the resources within the jar manually I could execute the jar)

Thanks in advance!

I have linked the sources into the workspace is that an issue?

UMY
  • 281
  • 1
  • 3
  • 8

1 Answers1

19

Mark the resource folder under your project's root folder as a "Source Folder" in Eclipse (right click on the folder, go to "Build Path" > "Use as source folder"). Then read the resources like this:

InputStream is = MyClass.class.getClassLoader().getResourceAsStream(name + ".SOURCE");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
  • If I do that the resources get packaged without the folder resources directly under the root folder. – UMY Aug 31 '11 at 21:52
  • You are right. I changed the code accordingly to remove `resources` from the resource path. If you want a directory called `resources` in your jar then create this folder hierarchy: `/resources/resources` and add `/resources` to eclipse source path as mentioned above. Also add back `resources` in the resource path in the code. – Abhinav Sarkar Aug 31 '11 at 21:56
  • This is such a simple thing, but without SO and your amazing answer, I would be fumbling around for so many hours... ***Thank you!!!!*** – Chris Cirefice Sep 26 '16 at 02:44
  • Worked like a charm :) – Wolf Nov 12 '16 at 13:29