8

I'm not being able to read a JSON file within resources directory from my project's compiled jar.

But... first things first:

I've created a 'Gradle Groovy' Project within Intellij idea (v2017.3.4 Community Edition) with the following structure:

enter image description here

So, as you can see, I just have a Main.groovy class and a resources folder inside src/main directory.

Inside resources directory I've a single resource db/products.json.

By going into File > Project Structure > Modules > Project Module > Sources menu we can check that src/groovy folder is marked as Sources and resources folder is marked as Resource Folder, so I can assure that this folders are correctly set on classpath:

enter image description here

On my Main class I've added following code line to load /db/products.json resource:

enter image description here

By running the Project within Intellij idea I can check that the resource is loaded seamlessly:

enter image description here

Great! But...

Now the catch!

When I compile project with gradle - through gradlew build command - the jar lib ReadJSONResourceFile-1.0-SNAPSHOT is generated inside build/libs folder.

Looking at the jar's structure, we can check that db resource folder is at the jar's root location:

enter image description here

But when I execute the jar through command:

groovy -cp build/libs/ReadJSONResourceFile-1.0-SNAPSHOT.jar -e "org.company.demo.Main.main()"

the resource is not successfully loaded. getResource() just returns null:

enter image description here

I've been trying to workaround this with other solutions provided here on Stack Overflowbut neither of them worked.

I'm missing something here... can you help?

Cristian Gonçalves
  • 892
  • 2
  • 9
  • 18
  • 4
    you are calling `getClass()` in a static method. maybe that's the problem? – daggett Mar 15 '18 at 16:26
  • It should be `getClass().getResource("/db/products.json")`. Does `getResource` accept a Map as an argument? – abdul Mar 15 '18 at 16:31
  • Yes @dagget, I believe that's the problem. I've created a `Java` application (instead of a groovy one) and when I typed `getClass()`, Intellij idea warned me right away that I can not call `getClass()` inside a `static method`. I then created a Singleton class to wrap `getClass().getResources()` call and things started to work just fine (runing app both through the IDE and through the jar) – Cristian Gonçalves Mar 15 '18 at 17:17
  • 1
    @abdul that's not a map inside `getResource()` arguments... it's just Intellij idea's intellisense in action! :) – Cristian Gonçalves Mar 15 '18 at 17:18
  • you can use `@groovy.transform.CompileStatic` annotation for groovy class to have static validation – daggett Mar 15 '18 at 17:27
  • good tip @daggett! Thanks for your help – Cristian Gonçalves Mar 15 '18 at 17:32
  • 1
    @dagget, late answer here :) You were right. The problem was the `getClass()` call inside the `main` static method. Making the call inside a `non-static method` works as expected. Thank you. – Cristian Gonçalves Apr 18 '18 at 14:45
  • Take a look at this article: https://www.javaworld.com/article/2077548/learn-java/java-tip-49--how-to-extract-java-resources-from-jar-and-zip-archives.html I was able to use that class to load an image (jpg) from Resources using groovy. – Ibrahim.H Jun 03 '18 at 14:24

0 Answers0