Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException with Maven dependency system scoped

I am getting ClassNotFoundException and NoClassDefFoundError exceptions when I attempt to run my application using a maven defined dependency.

I added my maven dependency for the jar in question to my pom.xml file with the following declaration:

<dependency>
  <groupId>ldap</groupId>
  <artifactId>com.novell.ldap</artifactId>
  <systemPath>${local.lib.dir}/ldap.jar</systemPath>
  <scope>system</scope>
  <version>1</version>
</dependency>

The jar is correctly added to the Dependencies NetBeans project

enter image description here

But when I deploy the app the dependency is missing

java.lang.NoClassDefFoundError: com/novell/ldap/LDAPException
like image 377
ezy Avatar asked Jul 28 '17 07:07

ezy


People also ask

What is scope in Maven dependency?

Dependency scopes can help to limit the transitivity of the dependencies. They also modify the classpath for different build tasks. Maven has six default dependency scopes. And it's important to understand that each scope — except for import — has an impact on transitive dependencies.

What is scope in dependency?

Dependency scope is used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath. There are 6 scopes: compile. This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project.

What is dependencyManagement in POM xml?

<dependencyManagement> allows to consolidate all dependencies (used at child pom level) used across different modules -- clarity, central dependency version management.


2 Answers

If you read the Maven documentation about this scope, it seems the expected behavior if your application server doesn't provide this library at runtime:

This scope is similar to provided except that you have to provide the JAR which contains it explicitly.

The provided scope states :

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime.

Not advised solution : add this library in the lib folder of your application server.
Cleaner solution : add this maven dependency in your maven repositories manually or with mvn install:install-file.
And remove the system scope of this dependency. It will use the default scope.

like image 168
davidxxx Avatar answered Oct 02 '22 12:10

davidxxx


The system scope in maven is somewhat like provided, that is dependency is used only at compile time. It 's your responsability to make sure that jar is in the classpath at runtime.

Besides, the system scope is actually deprecated, consider other alternatives. see introduction to dependency mechanism

like image 20
gtosto Avatar answered Oct 01 '22 12:10

gtosto