0

In a java project I have been given, at one point an external file is referenced. This file is held in a folder named resources. In the code, the call is:

DHKeyReader d = new DHKeyReader("resources/filename")

When run in Eclipse (3.7.2) this executes without failure. However, when exported to a jar, this fails with an IOException.

I am hesitant to modify the code, as it was given to me under the pretense that it worked and I was to familiarize myself with it so it could be altered. However, there still could be errors.

My question is this: how do I make a folder (held on the root of the .jar) visible within the jar like it is when running in Eclipse?

chancea
  • 5,858
  • 3
  • 29
  • 39
  • I am not sure but perhaps [this](http://stackoverflow.com/questions/17729794/exporting-a-java-project-with-external-dependencies-to-a-jar-using-eclipse) or [this](http://stackoverflow.com/questions/14694512/how-to-export-an-eclipse-project-with-external-jar-dependencies) might help? – chancea Jun 17 '14 at 19:03
  • If I remember correctly "normal" filepaths don't work the way you expect in a JAR. I *think* the normal idiom is `getClass().getResourceAsStream("relative_path")`, but I'm not 100% sure... – awksp Jun 17 '14 at 19:04

1 Answers1

0

This is because at runtime the classloader is not able to find the resource. Its always better to use : YourClass.class.getResourceAsStream(fileName), if you are using the resources in YourClass. This way the resource is searched by the class loader.

Vivin
  • 1,327
  • 2
  • 9
  • 28