resource-loader icon indicating copy to clipboard operation
resource-loader copied to clipboard

Don't work inside JAR

Open alabotski opened this issue 4 years ago • 3 comments

It's my resource folder image

My code

@Component
public class TableConfigFactory {

  private static final String UPLOAD_DIR = "upload";
...
  public TableConfigFactory(ObjectMapper objectMapper) throws IOException, URISyntaxException {
    this.objectMapper = objectMapper;

    URL url = ResourceLoader.getThePathToTheJarWeAreIn(TableConfigFactory.class);
    log.info("{}", url);

    Path uploadPath = FileLoader.get()
        .load(UPLOAD_DIR, TableConfigFactory.class)
        .toPath();
  }

When I run it in Idea all work fine When I run it from jar

I have such error

Caused by: java.io.IOException: Failed to list contents of file:/Users/XXX/Workspace/word/build/libs/word-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/upload

ResourceLoader.getThePathToTheJarWeAreIn(TableConfigFactory.class) return such string 12:08:27.882 [main] INFO com.test.TableConfigFactory - jar:file:/Users/XXX/Workspace/word/build/libs/word-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/

And

        // Test if we are in a JAR and if we are
        // then do the following...
        if (isJarFile(fullJarPathURL)) 

return false =(

alabotski avatar Jul 27 '21 09:07 alabotski

Inside the isJarFile method can you try and print the debug statement here?

private boolean isJarFile(URL jarUrl) {
     // ...
            catch (IOException | IllegalStateException | SecurityException e) {
                logger.debug("This is not a JAR file due to {}", e.getMessage()); <------- here
            }
    // ...
    }

Or maybe it doesn't throw an exception but fails due to lack of manifest file within the JAR due to this code:

private boolean isJarFile(URL jarUrl) {
             // ...
             // Successfully opened the jar file. Check if there's a manifest
             // This is probably not necessary
             Manifest manifest = jarFile.getManifest();
             if (manifest != null) {
                 return true;
             }

Do you have a manifest file inside your JAR?

gurpreet- avatar Aug 01 '21 10:08 gurpreet-

The problem was . That I had spring boot jar.

In normal jar structure, all work fine

alabotski avatar Aug 05 '21 05:08 alabotski

Is this problem resolved then @alabotski?

gurpreet- avatar Dec 22 '21 23:12 gurpreet-