7

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
1
  • system scope means that you need to make sure yourself that the jar file is in the classpath - Maven is not going to do it for you. If you don't make sure yourself that the jar containing the necessary class is available, then you get a NoClassDefFoundError. See: Dependency Scopes for a description of what the different Maven scopes mean. Commented Jul 28, 2017 at 7:50

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I see, thanks. Is there a way to specify this behaviour in the pom file? Knowing that the project will be shared I would like to make this solution as clear and painless as possible.
" Is there a way to specify this behaviour in the pom file?" If you remove systemPath and scope element of the dependency declaration, it will be ok for every Maven user of your company if you add/install the ldap.jar in your company central repository.
1

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

Comments

0

The problem is with your maven 'system' scope. It means that the dependency must be available on the application server where the application is deployed (same as scope 'provided'). Scope 'system' has the additional requirement that the dependency must be available as a jar file.

I would recommend to remove the scope element. Like this the dependency will be downloaded into your maven repository and will be available in your generated package file that can be deployed on your application server.

Another solution could be to put the jar file in the library folder of your application server.

Here's a link to the maven documentation concerning this issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.