Jar file not finding image but in Idea does

82 Views Asked by At

I'm trying to load an image in Java using Jaylib, it works in Intellij but not does as a Jar.

Method for Resource Locations:

@Nullable
    public String getPath() {
        try {
            return Path.of(ClassLoader.getSystemClassLoader().getResource(this.location).toURI()).toString();
        } catch (Exception e) {
            LOGGER.error("No object found at location!");
            LOGGER.debug(Arrays.toString(e.getStackTrace()));
        }

        return null;
    }

I tried using getClass().getResource, getClass().getClassLoader().getResource() and using a fat jar instead of a shadow jar.

Edit: I debugged it and this is the log:

[jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:156), jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:142), java.base/java.nio.file.Path.of(Path.java:208), com.github.command17.mariomaker17.resource.ResourceLocation.getPath(ResourceLocation.java:26), com.github.command17.mariomaker17.resource.Textures.loadTextures(Textures.java:9), com.github.command17.mariomaker17.Main.main(Main.java:42)]
1

There are 1 best solutions below

2
30thh On

IntelliJ has a different way to decide, which directories must be a part of the classpath. It is very possible, the directory with a image is tagged as a resource directory in the project setting, but not in the pom.xml Finally the file is not exported to the JAR at all.

You need to ensure, you specify the resource directory like this in your pom.xml:

<build>
   ...
   <resources>
     <resource>
       <directory>[your folder here]</directory>
     </resource>
   </resources>
   ...
</build>