1

I have a Maven project in Eclipse with a main() method. The project has a dependency that uses embedded resources, i.e. it contains files in /src/main/resources. I would like to create a runnable JAR file from the project. The problem is that - depending on how I load the embedded resources - I can either run the project as a runnable JAR using java -jar my.jar, or I can run it directly from Eclipse, but not both - the reason being that the path to these resources obviously differs between the two cases.

For example, I can load the resources as follows:

// Loads /src/main/resources/script.rb;
// Works when run directly from Eclipse; throws an exception when run as a runnable JAR
InputStream resource = this.getClass().getResourceAsStream("/script.rb");

This way, I can successfully run the project directly from Eclipse. Creating a runnable JAR using Eclipse's Export → Runnable JAR file puts the script.rb resource inside the resources folder in the JAR's root, which makes the above code fail when I try to run it as java -jar my.jar. I can fix this by changing the code to point to the /resources folder:

// Works when run as 'java -jar my.jar', fails when run from Eclipse.
InputStream resource = this.getClass().getResourceAsStream("/resources/script.rb");

Now, the exported JAR file works fine, but I can no longer successfully run my project as a Java application directly from Eclipse.

I read several other posts on loading resources, but nothing helped so far.

Is there a way to fix this and load the resource in a way that works correctly in both cases? What would probably work is convincing Eclipse to put resources inside the JAR's root instead of inside the resources folder, but I'm not sure if this is possible.

w128
  • 4,680
  • 7
  • 42
  • 65
  • 1
    src/main/resources is part of a Maven directory structure, yet apparently you're not using Maven to create the jar. Are you sure you're supposed to use the eclipse export jar function? – Gimby Oct 13 '16 at 14:56
  • @Gimby I'm using the export functionality simply because it's convenient for testing the JAR on a remote system. – w128 Oct 13 '16 at 14:58
  • "Export → Runnable JAR file puts the script.rb resource inside the resources folder in the JAR's root" That's WRONG!!! anything in `src/main/resources` should go in the root of the jar. You should let maven generate the jar – lance-java Oct 13 '16 at 14:59
  • Indeed it appears that using Eclipse to create the runnable JAR is not suitable here. For the record, I'm already using Maven to create the JAR too, but it's not directly runnable due to missing "main manifest attribute". I'll try to fix that. – w128 Oct 13 '16 at 15:08
  • http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven ;) – Gimby Oct 14 '16 at 06:55

1 Answers1

1

Using Class.getResourceAsStream(...) will load relative to the package. You should use Classloader.getResourceAsStream(...) to load from root.

Eg

InputStream resource = this.getClass().getClassloader().getResourceAsStream("script.rb");

(Assuming that src/main/resources is on the ecliplse classpath)

Note: the / has been removed!

Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Thank you for your answer, but unfortunately this way only works when I run the project from Eclipse - it fails just as before when I run it as an exported runnable JAR. – w128 Oct 13 '16 at 15:03
  • @w128 If you use Maven, don't export your jar manually with eclipse: generate it using Maven. – Olivier Grégoire Oct 13 '16 at 15:31