Gradle java mail class path problem

This code can not run

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'javax.mail:mail:1.4.1'
    }
}
  Class.forName("javax.mail.MessagingException")

here is the exception

Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
        at java_lang_Class$forName.call(Unknown Source)
        at build_6e6pqakh97nko2onijbdtbi3jd.run(build.gradle:10)
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:52)
        ... 32 more

Class.forName(“javax.mail.MessagingException”) is working well in my standalone test app. But seems that gradle classloader prevent to load this class. Do you have any ideas?

Thanks

I don’t know what’s going on here, but judging from the (full) stack trace, it might be some Groovy limitation. Anyway, ‘getClass().getClassLoader().loadClass()’ will do the trick.

Why do you need to load that class using reflection?

PS: Unless you need to get something onto the build script class path that is only available in ‘mavenLocal()’, I recommend to remove this repository. It will make your build less repeatable, and won’t lead to fewer downloads, as Gradle will steal artifacts from the local Maven repository in any case (if their checksum matches).

Hi Peter,

Thanks for your answering

I don’t use reflection This is just some snippet code that I provide to reproduce my ClassNotFoundException issue. Seems that getClass().getClassLoader().loadClass() is able to load this class but it still not fix my issue. But this helped me narrow the issue and fixed this :slight_smile:

Here is the project:

  • Custom plugin to send email (using BouncyCatlse mail that uses javax.mail.MessagingException). Absolutely this custom plugin depends on ‘javax.mail:mail:1.4.1’ - A buildSrc folder that has build.gradle using other dependencies but not ‘javax.mail:mail:1.4.1’ - A root build.gradle has ‘javax.mail:mail:1.4.1’

When I build my project it thowned “Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException”. When I added ‘javax.mail:mail:1.4.1’ to buildSrc build.gradle it worked well. I’m not sure that is an issue or not! It took me an age to narrow and figure out that!

PS: I used my custom plugin in Maven Local so I need this :slight_smile:

Hard to say from here what’s going on. Sounds like transitive dependency information of your plugin might be incorrect, and hence ‘javax.mail’ doesn’t make it on the build script class path.