Java.lang.NoClassDefFoundError : Failed resolution of: Ljavax/naming/directory/InitialDirContext; mistake
I’m trying to build a chat application with the XMPP protocol using the Smack library in Android Studio.
By using Smack version 3.1.0, I could easily connect to the Open Fire server, and even I sent a message to a user logged in through Spark. But I could never get a message from that user because MessageListener’s processMessage only ran once at compile time. Because I couldn’t get my favorite results, I searched the internet and found that smack is from version 4.1 or later, only supports android.
So I tried to build my app with smack 4.1.4 and solved many bugs.
But there is a bug that is not solved and I can’t find it on the internet.
That’s it (of course there are more bugs, but I brought the first 4):
11-10 00:16:03.446 15512-15539/xmppconnectiontest2.ffisher.com.xmpp2 E/AndroidRuntime: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/naming/ directory/InitialDirContext;
11-10 00:41:36.169 19859-19886/xmppconnectiontest2.ffisher.com.xmpp2 E/AndroidRuntime: at org.jivesoftware.smack.util.dns.javax.JavaxResolver.<clinit>( JavaxResolver.java:50)
11-10 00:41:36.169 19859-19886/xmppconnectiontest2.ffisher.com.xmpp2 E/AndroidRuntime: at java.lang.Class.classForName(Native Method)
11-10 00:41:36.169 19859-19886/xmppconnectiontest2.ffisher.com.xmpp2 E/AndroidRuntime: at java.lang.Class.forName(Class.java:308)
As the igniterealtime documentation says, I added all the necessary libraries.
Here are my dependencies in Gradle:
compile 'org.igniterealtime.smack:smack-core:4.1.4'
compile 'org.igniterealtime.smack:smack-tcp:4.1.4'
compile 'org.igniterealtime.smack:smack-extensions:4.1.4'
compile 'org.igniterealtime.smack:smack-resolver-javax:4.1.4'
compile 'org.igniterealtime.smack:smack-bosh:4.1.4'
compile 'org.igniterealtime.smack:smack-resolver-dnsjava:4.1.4'
compile 'org.igniterealtime.smack:smack-legacy:4.1.4'
compile 'org.igniterealtime.smack:smack-experimental:4.1.4'
compile 'org.igniterealtime.smack:smack-jingle-old:4.1.4'
compile 'org.igniterealtime.smack:smack-debug:4.1.4'
Main activity at creation time:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
String [] param = {"mhs", "10.0.2.2"};
Asynchron asynchron = (Asynchron) new Asynchron();
asynchron.doInBackground(param);
}
});
t.start();
Subclass of AsyncTask I use:
public class Asynchron extends AsyncTask <String, Void, Void>
{
@Override
protected Void doInBackground(String... params) {
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setServiceName(params[0]);
builder.setHost(params[1]);
builder.setPort(5222);
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
builder.setDebuggerEnabled(true);
builder.setCompressionEnabled(true);
connection = new XMPPTCPConnection(builder.build());
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
try {
connection.connect();
connection.login("testUser1", "1234");
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
return null;
}
}
I really need your help.
How do I fix the error I wrote in the title of the issue?
Solution
Just use:
compile "org.igniterealtime.smack:smack-android-extensions:4.1.0"
compile "org.igniterealtime.smack:smack-experimental:4.1.0"
compile "org.igniterealtime.smack:smack-tcp:4.1.0"
and remove builder.setCompressionEnabled(true);