简体   繁体   中英

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

I have a problem with the executable jar created from eclipse, that is, when I create it and run it from the command console I get the following error:

console error

And precisely this error in console, it does not launch it from the eclipse, just run the application without errors. The error in the generated jar (console error) is regarding the image located at: src/main/resources/logo.png , which can't seem to find the specified path.

Inside eclipse, I load the image with the following line: setIconImage((new ImageIcon(getClass().getClassLoader().getResource("logo.png"))).getImage()); , and the funny thing is that I don't get any error about NullPointerException .

For this reason, i follow the way of creating the executable jar from maven, doing a: mvn clean compile assembly:single (creates an executable jar with the dependencies included), executing it without problem.

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.

It may be a problem with the maven folder structure, the.classpath file or some wrong configuration. Whatever, here is the full pom.xml , for reference:

<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>

The .classpath file:

<?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>

And this is the folder structure of the project:

folder structure

This problem is driving me crazy, I hope some help. Thank you!

Probably reason is you pack the image within your jar file. If image is let say at /resource/img you have to put a resource/img file next to your jar like this

app.jar
resource/img/...

side note: jar files are read-only files if you try to do any thing to a image file inside your jar file it will fail

According to the docs accessing resources via the Class or ClassLoader methods which return a URL (as opposed to an InputStream ) might fail due to certain security reasons.

It also states that getResource() will always succeed if the resource is actually in the JAR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM