簡體   English   中英

從 eclipse 創建的可運行 jar 無法運行,並且使用 Z402C5D9D9AF6B43F714EA07 構建可執行文件 jar 如果它運行,為什么?

[英]The runnable jar created from eclipse does not run and building the executable jar with maven if it runs, why?

我對從 eclipse 創建的可執行 jar 有問題,也就是說,當我創建它並從命令控制台運行它時,我收到以下錯誤:

控制台錯誤

正是控制台中的這個錯誤,它不會從 eclipse 啟動它,只需運行應用程序而不會出現錯誤。 生成的 jar (控制台錯誤)中的錯誤與位於src/main/resources/logo.png的圖像有關,該圖像似乎找不到指定的路徑。

在 eclipse 中,我使用以下行加載圖像: setIconImage((new ImageIcon(getClass().getClassLoader().getResource("logo.png"))).getImage()); ,有趣的是我沒有收到任何關於NullPointerException的錯誤。

出於這個原因,我按照從 maven 創建可執行 jar 的方式,執行: mvn clean compile assembly:single (創建一個可執行 jar 包含的依賴項沒有問題)。

I'm sure the problem is with the classpath, since when creating the jar from eclipse: Export... > Java > Executable JAR file, once created it does not find the image.

可能是 maven 文件夾結構、.classpath 文件或某些錯誤配置有問題。 不管怎樣,這里是完整的pom.xml ,供參考:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- Definicion del artefacto -->
    <groupId>com.silentsoft</groupId>
    <artifactId>CalculadoraAO</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>CalculadoraAO</name>
    <description>Calculadora basada en ImperiumAO</description>
    <url>https://github.com/rusocode/CalculadoraAO</url>

    <!-- Propiedades del compilador -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <licenses>
        <license>
            <name>GNU GPL v3.0</name>
            <url>https://www.gnu.org/licenses/</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>Ru$o</id>
            <name>Juan Debenedetti</name>
            <email>juandebenedetti94@gmail.com</email>
            <roles>
                <role>owner</role>
                <role>developer</role>
            </roles>
        </developer>
    </developers>

    <dependencies>
        <!-- https://search.maven.org/artifact/com.miglayout/miglayout/3.7.4/jar -->
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>3.7.4</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://search.maven.org/artifact/org.swinglabs.swingx/swingx-autocomplete/1.6.5-1/jar -->
        <dependency>
            <groupId>org.swinglabs.swingx</groupId>
            <artifactId>swingx-autocomplete</artifactId>
            <version>1.6.5-1</version>
        </dependency>


    </dependencies>

    <!-- Configura maven-compiler-plugin para usar la misma codificacion de 
        caracteres en la que estan codificados los archivos fuente. -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <!-- Plugin para generar el .jar con las dependencias includidas -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.silentsoft.Launcher</mainClass> <!-- Indica la clase con el metodo main -->
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

.classpath文件:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>

    <classpathentry kind="src" output="target/classes"
        path="src/main/java">
        <attributes>
            <attribute name="optional" value="true" />
            <attribute name="maven.pomderived" value="true" />
        </attributes>
    </classpathentry>

    <classpathentry excluding="**" kind="src"
        output="target/classes" path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true" />
        </attributes>
    </classpathentry>

    <classpathentry kind="src" output="target/test-classes"
        path="src/test/java">
        <attributes>
            <attribute name="test" value="true" />
            <attribute name="optional" value="true" />
            <attribute name="maven.pomderived" value="true" />
        </attributes>
    </classpathentry>

    <classpathentry kind="con"
        path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true" />
        </attributes>
    </classpathentry>

    <classpathentry kind="con"
        path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true" />
        </attributes>
    </classpathentry>

    <classpathentry kind="output" path="target/classes" />

</classpath>

這是項目的文件夾結構:

文件夾結構

這個問題快把我逼瘋了,希望能有所幫助。 謝謝!

可能的原因是您將圖像打包在 jar 文件中。 如果圖像在 /resource/img 你必須像這樣在你的 jar 旁邊放一個資源/img 文件

app.jar
resource/img/...

旁注:jar 文件是只讀文件,如果您嘗試對 jar 文件中的圖像文件執行任何操作,它將失敗

根據通過ClassClassLoader方法訪問資源的文檔,這些方法返回URL (而不是InputStream )可能由於某些安全原因而失敗。

它還指出,如果資源實際上位於 JAR 中,則getResource()將始終成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM