Jar文件没有找到图像,但在Idea中找到了



我正在尝试使用Jaylib在Java中加载图像,它在Intellij中工作,但不作为Jar。

资源位置方法:

@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;
}

我尝试使用getClass().getResource,getClass().getClassLoader().getResource()和使用胖罐而不是阴影罐。

编辑:我调试了它,这是日志:

[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)]

IntelliJ有一种不同的方式来决定哪些目录必须是类路径的一部分。这是很有可能的,带有图像的目录在项目设置中被标记为资源目录,但在pom.xml中没有,最后文件根本没有导出到JAR。

您需要确保在pom.xml中像这样指定资源目录:

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

相关内容

最新更新