pom.xml that works (Could not find or load main class/no main manifest attribute)

Viewed 141

I have spent several hours on this and I'm unable to get it done. For a Maven/JavaFX-Application I am trying to get an executable jar. But whenever I build it with its dependencies and execute it with java -jar Kvn-jar-with-dependencies.jar I get one of these errors:

When executing the Fat Jar:

  • Error: Could not find or load main class com.qohelet.kvn.MainApp

When executing the Jar which apparently doesn't contain the dependencies

  • no main manifest attribute, in Kvn.jar

I use netbeans 8.2 for development (this is one of the last versions which can run with the OpenJDK 1.8.0) and I can run my project any time using (or better, Netbeans uses it):

cd /home/qohelet/Projekte/Kvn; JAVA_HOME=/home/qohelet/jdk1.8.0_111 /home/qohelet/netbeans-8.2/java/maven/bin/mvn "-Dexec.args=-classpath %classpath com.qohelet.kvn.MainApp 'USER' 1 'Indonesia'" -Dexec.executable=/home/qohelet/jdk1.8.0_111/bin/java org.codehaus.mojo:exec-maven-plugin:1.2.1:exec

There had been an issue with the obtaining the dependencies due to the change by mvn to ssh, but I didn't even have maven installed before, it still worked. (Maven dependencies are failing with a 501 error), now one of the addresses in the project header contains a https instead of a http but I don't think this is much of an issue.

Currently the pom.xml looks like this, this is an edited version of https://maven.apache.org/plugins/maven-assembly-plugin/usage.html:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.qohelet</groupId>
    <artifactId>Kvn</artifactId>
    <version>2.2</version>
    <packaging>jar</packaging>
    <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>
    <build>
        <finalName>Kvn</finalName>
        <plugins>                
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>                               
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>              
                            <mainClass>com.qohelet.kvn.MainApp</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>
    </build>
    <dependencies>
        <!-- Various -->
    </dependencies>
    <name>Kvn</name>    
</project>

This version gives me the mentioned error.

I found a different suggestion where the -Section is set up like that which apparently worked for a while, but for some reason it doesn't seem to be advised as Netbeans complains:

Problem: Project Problem: Project's main artifact is processed through maven-shade-plugin, resolvable by: org.netbeans.modules.maven.problems.ProblemReporterImpl$MavenProblemResolver@a5534d unresolved

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.qohelet.kvn.MainApp</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

A few months ago the project could still be built with the following -Section but this was mostly a result from trial and error until I had a working result:

<build>
        <finalName>Kvn</finalName>
        <plugins>

            <!-- download source code in Eclipse, best practice -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
            </plugin>

            <!-- Maven Shade Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <!-- add Main-Class to manifest file -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.qohelet.kvn.MainApp</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>    
        </plugins>
    </build>

I am not sure if I interpret this correctly but can it be due to version changes in Java? SO provides a lot of answer, but I am not able to get anything to work.

https://stackoverflow.com/a/589111/2516892 gets me the exact same error:

<build>
    <plugins>
          <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                  <archive>
                      <manifest>
                          <addClasspath>true</addClasspath>
                          <mainClass>com.qohelet.kvn.MainApp </mainClass>
                      </manifest>
                  </archive>
                  <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
              </configuration>
              <executions>
                  <execution>
                      <id>make-my-jar-with-dependencies</id>
                      <phase>package</phase>
                      <goals>
                          <goal>single</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
    </plugins>
</build>

Similar to https://stackoverflow.com/a/4323501/2516892 with the following build section which doesn't return a Fat Jar but the same error.

What is the state of the art? How should it be done properly?

Thank you

Edit (1): The structure of the Jar looks fine to me. 22Mb seems to be a reasonable size too. It a folder /com/qohelet/kvn/ which contains a MainApp.class

Using https://stackoverflow.com/a/574650/2516892 and compiling with

mvn clean compile assembly:single

weirdly results in a set of errors like

package javafx.beans.value does not exist

Which is odd as JavaFX is installed and I can run the program without any issue. Netbeans is not doing much different:

cd /home/qohelet/Projekte/Kvn; JAVA_HOME=/home/qohelet/jdk1.8.0_111 /home/qohelet/netbeans-8.2/java/maven/bin/mvn clean install

But this results in a jar with only 254Kb, which I assume doesn't contain the dependencies. Including the make-assembly-part just creates the same error.

Using the 2nd one listed here https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html is just the same error: Error: Could not find or load main class com.qohelet.kvn.MainApp, the archive is slightly smaller (19Mb) and again it contains a folder-structure: /com/qohelet/kvn/ which contains a MainApp.class

0 Answers
Related