首页 > 解决方案 > 使用 Maven 构建带有 res 文件夹的 jar

问题描述

我正在使用 Slick2D 创建一个非常简单的游戏以与我的朋友分享。但是我无法打包 JAR 文件。JAR 构建完成,我尝试运行 JAR,但它崩溃了。使用命令提示符运行它后,我收到此错误Exception in thread "main" java.lang.RuntimeException: Resource Not found ./src/res/playbutton.png

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.thirdspare</groupId>
<artifactId>zurvive</artifactId>
<version>1.0-SNAPSHOT</version>


<build>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>7</source>
                <target>7</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.thirdspare.zurvive.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>

    <resources>
        <resource>
            <directory>src/main/java</directory>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/res</directory>
            <filtering>false</filtering>
        </resource>
    </resources>

</build>


<dependencies>
    <dependency>
        <groupId>org.slick2d</groupId>
        <artifactId>slick2d-core</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

我的 pom.xml 文件中是否缺少某些内容?我尝试了一些不同的方法来尝试将它们全部构建在一起,我只是想知道我是否错过了一些东西。

标签: javamavenjar

解决方案


资源不是文件,而是类路径上的条目。您应该完全使用它在 jar 文件中的名称。

在您的情况下,这将/playbutton.png取决于playbutton.png您的使用情况。


推荐阅读