Search code examples
javaguava

Issue with loading of resource file from jar file


I have an app that has such source code layout:

src
 main
  java
   com
    mypackage
     MyClass.java
  resources
    queries
      query.sql

in my MyClass I can load resource query.sql with

URL url = Resources.getResource("queries/query.sql");
String query = Resources.toString(url, Charset.UTF-8);

with Guava library when I run it from my IDE locally.

But when I build jar there is such structure:

myjar.jar
  com
    mypackage
      MyClass.class
  queries
    query.sql

After deployment to server I have:

IllegalArgumentException: resource queries/query.sql not found.

Is it possible to resolve this issue if I don't want to put sql script to mypackage package keeping it in separate folder? Thanks.


Solution

  • Copy of @VGR's comment here:

    The proper invocation is: MyClass.class.getResource("/queries/query.sql")