【问题标题】:The runnable jar created from eclipse does not run and building the executable jar with maven if it runs, why?从 eclipse 创建的可运行 jar 不运行,如果运行,用 maven 构建可执行 jar,为什么?
【发布时间】:2020-12-02 15:31:33
【问题描述】:

我从 eclipse 创建的可执行 jar 有问题,也就是说,当我创建它并从命令控制台运行它时,我收到以下错误:

console error

正是控制台中的这个错误,它不会从 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包括),执行它没有问题。

我确定问题出在类路径上,因为从 eclipse 创建 jar 时:Export ... > Java > Executable JAR file,一旦创建它就找不到图像。

可能是 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>

这是项目的文件夹结构:

folder structure

这个问题快把我逼疯了,希望有所帮助。谢谢!

【问题讨论】:

    标签: java eclipse maven jar


    【解决方案1】:

    可能的原因是您将图像打包到 jar 文件中。如果图像在 /resource/img 你必须像这样在你的 jar 旁边放一个资源/img 文件

    app.jar
    resource/img/...
    

    附注:jar 文件是只读文件,如果您尝试对 jar 文件中的图像文件执行任何操作,它将失败

    【讨论】:

      【解决方案2】:

      根据docs 通过Class 或ClassLoader 方法访问资源,返回URL(而不是InputStream)可能由于某些安全原因而失败。

      它还指出,如果资源实际上在 JAR 中,getResource() 将始终成功。

      【讨论】:

        猜你喜欢
        • 2014-04-01
        • 1970-01-01
        • 2017-08-15
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-02
        • 2012-03-06
        相关资源
        最近更新 更多