已添加Jar包仍出现JSONArray类未找到异常(NoClassDefFoundError)求助
java.lang.NoClassDefFoundError: org/json/JSONArray in Servlet Projects Hey there, let's work through this java.lang.NoClassDefFoundError: org/json/JSONArray problem you're hitting. You mentioned you've added the relevant JAR, but the runtime still can't find the class—this usually boils down to the JAR not being properly available to your application when it runs, not just during compilation. Here are actionable steps to fix it:
Ensure the JAR is in the correct runtime directory
For web/servlet projects, adding the JAR to your IDE's build path is only half the battle. You need to place it directly in your project'sWEB-INF/libfolder. If you're using an IDE like Eclipse or IntelliJ, confirm the JAR is included in your deployment assembly (Eclipse: Right-click project > Properties > Deployment Assembly; IntelliJ: File > Project Structure > Artifacts) to make sure it gets packaged into your WAR file.Use build tools to manage dependencies (if applicable)
If you're using Maven or Gradle, let the tool handle dependency management to avoid manual errors:- For Maven, add this to your
pom.xml:<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20240303</version> <!-- Use the latest stable version --> </dependency> - For Gradle, add this to your
build.gradle:implementation 'org.json:json:20240303'
Run a build (
mvn clean installorgradle build) to ensure the JAR is properly downloaded and included in your deployment.- For Maven, add this to your
Check for conflicting JARs
Conflicts often happen if multiple versions of the JSON library (or other libraries that includeorg.jsonclasses) exist in your classpath. Search your project'sWEB-INF/liband your application server's globallibdirectory for any duplicate or outdated JARs containingorg/json/JSONArray, then remove the redundant ones.Clean and rebuild your project
IDEs sometimes cache old build artifacts that cause issues. Perform a full clean:- Eclipse: Go to
Project > Cleanand select your project - IntelliJ: Go to
Build > Clean Project
Then rebuild your project and redeploy it to your server.
- Eclipse: Go to
Verify the JAR itself is valid
It's possible the JAR you added is corrupted or doesn't actually contain theJSONArrayclass. Download a fresh copy of the official org.json JAR, open it with a zip tool, and confirm theorg/json/JSONArray.classfile exists inside.
内容的提问来源于stack exchange,提问作者Raghavendra

