Fixing Maven ClassNotFoundException for json-lib by Adjusting Classifier and outputFileNameMapping
This article explains why a Maven build fails with a ClassNotFoundException for net.sf.json.JSONException due to a missing classifier in the json-lib jar, and shows how to modify the assembly plugin's outputFileNameMapping to correctly include the classifier, preventing the runtime error.
The author frequently encounters Maven-related problems and links to two previous articles that discuss common Maven dependency issues and snapshot version usage.
In this post, a new error is presented: a java.lang.ClassNotFoundException: net.sf.json.JSONException stack trace, indicating that the json-lib library could not be loaded at runtime.
The Maven dependency declared is:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>Although the jar json-lib-2.4.jar exists in the packaged lib directory, the required classifier makes the correct file name json-lib-2.4-jdk15.jar . Because the packaged jar lacks the jdk15 suffix, the class loader cannot find the needed classes.
The root cause is the project's assembly plugin configuration, which uses a fixed outputFileNameMapping that does not incorporate the classifier:
<outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}.${artifact.extension}</outputFileNameMapping>Since the classifier is omitted, the resulting jar name is wrong. The solution is to modify the mapping to include the classifier when present:
<outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}</outputFileNameMapping>This expression adds the classifier (with a preceding dash) only if it exists, ensuring the jar is named json-lib-2.4-jdk15.jar and the ClassNotFoundException is resolved.
For further details, see the Maven EAR plugin documentation: http://maven.apache.org/plugins/maven-ear-plugin/examples/customize-file-name-mapping.html
About the author: Yin Jihuan, a technology enthusiast and author of "Spring Cloud Microservices – Full‑Stack Technology and Case Analysis" and "Spring Cloud Microservices: Beginner, Practice, and Advanced", founder of the WeChat public account "Yuan Tiandi".
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.