How to know where the class is coming from in Java

Mohammed R. El-Khoudary :

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?

Mohammed R. El-Khoudary :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=131772&siteId=1