简体   繁体   中英

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.

Copy of @VGR's comment here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM