使用Smack API连接OpenFire服务器时出现ClassNotFoundException报错
NoClassDefFoundError: org/kxml2/io/KXmlParser with Smack & OpenFire Hey there, let's break down this error you're facing—this is a super common issue when working with older versions of Smack, and it's almost always tied to missing dependencies.
What's causing this?
The NoClassDefFoundError means your Java runtime can't find the KXmlParser class, part of the kxml2 library. Older Smack versions (like the 3.x series, which uses XMPPConnection instead of the newer XMPPTCPConnection) rely on kxml2 for XML parsing. If that library isn't in your project's classpath, you'll hit this exact error.
Step-by-step fixes
1. Add the kxml2 dependency
How you do this depends on your project's dependency management setup:
If using Maven: Add this to your
pom.xml:<dependency> <groupId>net.sf.kxml</groupId> <artifactId>kxml2</artifactId> <version>2.3.0</version> </dependency>If using Gradle: Add this to your
build.gradle(Groovy):implementation 'net.sf.kxml:kxml2:2.3.0'If managing jars manually: Download the kxml2 jar (version 2.3.0 works best with most Smack 3.x versions) and drop it into your project's
libfolder. Then make sure your IDE (Eclipse/IntelliJ) has it added to the build path—right-click the jar and select "Add as Library" if needed.
2. Double-check all Smack dependencies
Older Smack versions have a few other required dependencies too. To avoid missing more classes, pull in the full Smack core and extensions dependencies instead of individual jars:
For Maven:
<dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smack</artifactId> <version>3.4.1</version> <!-- Replace with your Smack version --> </dependency> <dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smackx</artifactId> <version>3.4.1</version> </dependency>
3. Verify your classpath
Sometimes even after adding dependencies, your IDE might not include them in the runtime classpath. In IntelliJ, go to File > Project Structure > Modules > Dependencies and ensure all required jars are marked as "Compile" or "Runtime". In Eclipse, right-click your project > Build Path > Configure Build Path and check the "Libraries" tab.
Bonus: Upgrade to a newer Smack version (recommended)
The version of Smack you're using (with XMPPConnection) is outdated and no longer maintained. If you can, switch to Smack 4.x+—it has a cleaner API, better dependency management, and doesn't rely on kxml2 anymore. Here's a quick connection example with Smack 4.4:
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; public class Main { public static void main(String[] args) { try { XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() .setHost("your-openfire-server-ip") .setPort(5222) .setXmppDomain("your-openfire-domain") // e.g., "localhost" for local servers .setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled) // Disable if not using SSL .build(); XMPPConnection connection = new XMPPTCPConnection(config); connection.connect(); System.out.println("Connected to OpenFire successfully!"); // Optional: Login // connection.login("username", "password"); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
For Maven, the dependency for Smack 4.4 is:
<dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smack-tcp</artifactId> <version>4.4.6</version> <!-- Latest stable version as of now --> </dependency>
内容的提问来源于stack exchange,提问作者JohnCalms

