1

When I mvn clean package the project and run it with java -jar target/project.jar it throws an error that it cannot find some class from an external jar.

Steps taken

  1. All external jars are in a folder of my project: /jars or /jars/morejars/

  2. Adding the jars to the build path: In Eclipse I right click on project, go to build path and select add external archives. I can see that eclipse creates a library folder "referenced libraries" below the "Maven dependencies" folder. When I check project -> Properties -> Java Build Path -> Libraries I can see all the imported jars.

Some of those external jars are described as <dependency> (with <systemPath) in pom.xml, so they will not be seen in Referenced Libraries but in Maven dependencies (interestingly, the class that cannot be found when running my packaged project is in an external jar that doesn't reside in Referenced Libraries but Maven dependencies)

e.g.

       <dependency>
            <groupId>external.jar.groupid</groupId>
            <artifactId>external.jar.artifactid</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/jars/external-jar.jar</systemPath>
        </dependency>
  1. Use the maven assembly plugin (as described here), this is my full <build> config:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                  <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
                <configuration>
                  <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
                </configuration>
              </plugin>
        </plugins>
    </build>
    
  2. Run mvn clean package -> BUILD SUCCESS

  3. java -jar target/project.jar -> Cannot find some.class from external.jar

Thanks for the help

tenticon
  • 2,639
  • 4
  • 32
  • 76
  • 2
    Is there a specific reason why you use `system` scoped dependencies? "..They are usually used to tell Maven about dependencies which are provided by the JDK or the VM.." – Marinos An Apr 24 '18 at 10:40
  • 1
    I also guess the system scope can be the root of cause, because its useful eg. when you have some shared libs eg. in application server libs folder, etc.. but not for standalone apps- check [maven doc](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies) `Dependencies with the scope system are always available and are not looked up in repository. They are usually used to tell Maven about dependencies which are provided by the JDK or the VM. ` So during the package target they will not be downloaded/ added to your package – xxxvodnikxxx Apr 24 '18 at 10:50

1 Answers1

2

The problem is clearly with dependencies. While your application starts loading it looking for required classes to run properly. While it loading a class from a jar library and that library class also refer to some other class from a another jar library file but that library (jar file) is not available in your class path, it will give you the above error. So check for maven dependency and get all the jars required by your application. Also based on error message also you can add the jar reference in your pom.xml file. For example if you get an error like Failed to load com.apache.some.Example.class just google the Example.class jar file and get it from maven repository.

Hope this will help you.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17