Mohammed R. El-Khoudary
Mohammed R. El-Khoudary

Reputation: 1203

How to know where the class is coming from in Java

I faced a very confusing problem today where my email service, I am using simple-java-mail, stopped sending all in a sudden.. and an exception was raised:

Exception in thread "pool-3-thread-1" java.lang.NoSuchMethodError: com.sun.mail.util.TraceInputStream.(Ljava/io/InputStream;Lcom/sun/mail/util/MailLogger;)V

I was going insane as nothing affecting emails was added, I tried to use maven enforcer plugin to discover what was going on but with no clue.

The question was how can I discover which jar or library does a class at runtime come from?

Upvotes: 2

Views: 63

Answers (1)

Mohammed R. El-Khoudary
Mohammed R. El-Khoudary

Reputation: 1203

With this snippet you can figure out from where this class is coming from, and so find the source of a class immediately:

    Class klass = Class.forName("javax.mail.Service");
    URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");

    System.out.println(location);

Now you can know javax.mail.Service resides in which JAR.

Upvotes: 2

Related Questions