Problems running executable jar with dependencies

2019-06-23 21:37发布

问题:

Hey so I have been working on a project that I want to be able to run as an executable jar from the command line. I have been able to create the jar with dependencies using Mavens assembly:single command. My pom looks like this.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>org.openmetadata.main.OmadUpdate</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

The build is successful and creates the jar omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar. I go to my projects target folder in the command line and type

java -jar omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar

I have also tried

java -cp omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar org.openmetadata.main.OmadUpdate

Unfortunately in each case I am given a java.lang.NoClassDefFoundError: org/openmetadata/main/OmadUpdate. I am confused because I know my main class is in the package org.openmetadata.main and yet it is not found. I find this especially confusing because in my pom I specify that class as my main class. I have tried changing the name of the main class to src.main.java.org.openmetadata.main.OmadUpdate and simply OmadUpdate as well but neither seems to have an effect. Thanks for any help in advance.

回答1:

I do not see a Class-Path entry in the manifest above, but your very long filename mentions dependencies. If there are jars within this jar file that your program is dependent on, you must enumerate them in the Class-Path section. See Adding Classes to the JAR File's Classpath for more details.



回答2:

Another option might be to use the onejar-maven-plugin. Unfortunately the usage page is a bit scarce, but the plugin does what is supposed to when configured correctly.



回答3:

I finally have been able to get this to work by adding the following code to my pom.

    <build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>org.openmetadata.omadupdate.OmadUpdate</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </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>

Without the executions tag in the pom along with its children only the maven dependencies will be added to the jar and the classes from the project itself will not be added.