Grails项目启动报错:Package javax.mail does not exist 求助
Hey Alex, let’s sort out this frustrating error—you mentioned your Grails project compiles fine, but blows up with Package javax.mail does not exist when you run run-app. Here are the most likely fixes tailored to your scenario:
1. Check Your Dependency Scope
The top culprit here is almost always a dependency scope that only includes javax.mail during compilation, not runtime.
For Grails 3+ (Gradle-based): Open your
build.gradleand ensure your javax.mail dependency usesimplementation(notcompileOnly) if your code directly references it. You’ll also need a runtime implementation jar since the API alone won’t work at startup:// For code that imports javax.mail classes implementation 'javax.mail:javax.mail-api:1.6.2' // Required runtime implementation runtimeOnly 'com.sun.mail:javax.mail:1.6.2'For Grails 2.x (BuildConfig.groovy): Update your dependencies block to use
compileinstead ofprovidedorcompileOnly:dependencies { compile 'javax.mail:javax.mail-api:1.6.2' runtime 'com.sun.mail:javax.mail:1.6.2' }
Scopes like compileOnly or provided tell Grails to use the dependency for compiling code but exclude it from the runtime classpath—which exactly explains why your build succeeds but startup fails.
2. Clear Grails/Runtime Caches
Your startup command shows Grails is using a temporary classpath jar (C:\Users\Alex\AppData\Local\Temp\classpath1564081530.jar). These cached artifacts can get out of sync with your actual dependencies:
- Run
grails clean(Grails 2.x) or./gradlew clean(Grails 3+) to wipe cached build files. - Rebuild your project with
grails compileor./gradlew compileGroovy, then try starting it again.
3. Verify Dependency Resolution
Double-check that javax.mail is actually being pulled into your runtime classpath:
- For Grails 3+: Run
./gradlew dependenciesand look forjavax.mailunder theruntimeClasspathorimplementationsections. - For Grails 2.x: Run
grails dependency-reportand confirm the jar is listed under "Compile" or "Runtime" dependencies.
If it’s missing, you’ll need to add the dependency explicitly (as shown in step 1) instead of relying on transitive resolution from another library.
4. Check for IDE-Specific Issues (If Applicable)
If you’re running the app from an IDE like IntelliJ or Eclipse, the IDE’s cached classpath might not match Grails’ build output:
- Refresh the Gradle/Grails project in your IDE.
- Run
grails cleanfrom the command line, then reimport the project to sync dependencies.
内容的提问来源于stack exchange,提问作者Alex432

