Answer the question
In order to leave comments, you need to log in
How to build jar from a project that uses maven jar?
All the best!
There is a small Java class that uses 1 Java library (jar file).
Locally, everything is good, everything works, the Maven build is also done, everything is OK, BUT it does not find the library
Run: java -jar Ver-1.0.jar
Gives an error:
Exception in thread "main" java.lang.NoClassDefFoundError:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Ver</groupId>
<artifactId>Ver</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>libs</groupId>
<artifactId>lib-1</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>
${project.basedir}/src/main/java/lib/lib-1.jar
</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>pc.Start</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>lib-1</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>libs</groupId>
<artifactId>lib-1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${project.basedir}/src/main/java/lib/lib-1.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Answer the question
In order to leave comments, you need to log in
The easiest way out of this situation is to build a fat jar that includes the classes of both the program itself and all its dependencies. You can do this by expanding pom.xml a bit:
<project ... >
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<имя>-<версия>.jar
and a thick one with a view name. <имя>-<версия>-jar-with-dependencies.jar
<project ... >
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
mvn dependency:copy-dependencies
, by which Maven will copy all the dependencies to target/dependency. After that, you can copy the program jar file and the dependency jar files into the same working directory and run the program. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question