Problems with an executable bank with dependencies

Hey, so I worked on a project that I want to run as an executable jar from the command line. I was able to create a jar of 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 jar omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar. I go to my project target folder on the command line and type

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

I also tried

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

Unfortunately, in each case, I'm assigned java.lang.NoClassDefFoundError: org / openmetadata / main / OmadUpdate. I am confused because I know that my main class is in the package org.openmetadata.main, and yet it was not found. I find this particularly confusing because in my pom I am specifying this class as my main class. I tried changing the name of the main class to src.main.java.org.openmetadata.main.OmadUpdate and just OmadUpdate, but none of them have an effect. Thanks for any help in advance.

+4
source share
3 answers

I do not see the Class-Path entry in the manifest above, but your very long file name mentions the dependencies. If there are bans in this jar file depending on your program, you should list them in the Class-Path section. See Adding classes to the JAR class path for more details.

+2
source

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

0
source

I was finally 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 exec tag in pom, only maven dependencies will be added to its jar with its children, and classes from the project itself will not be added.

0
source

Source: https://habr.com/ru/post/1403976/


All Articles