如何让IntelliJ识别Manifest中的Automatic-Module-Name?
Hey, I've run into exactly this issue before—everything compiles and runs perfectly fine, but IntelliJ stubbornly flags the requires directive in module-info.java and related import statements as errors. Total headache, right? Here are some tried-and-true solutions that worked for me:
1. Force a Full Project Resync & Cache Clear
More often than not, this is just an IDE cache or sync glitch. Try these steps:
- If you're using Maven/Gradle, click the sync button in the top-right corner of the IDE (the circular arrows icon);
- Or use the menu options:
File → Sync Project with Gradle Files(for Gradle) /File → Sync Project with Maven Projects(for Maven); - For stubborn cases, go to
File → Invalidate Caches..., check the box for "Clear file system cache and local history", then restart IntelliJ. This wipes out all wonky cached data that might be causing the misbehavior.
2. Verify the JAR is on the Modulepath (Not Classpath)
Automatic modules only work when the JAR is placed on the modulepath, not the traditional classpath. Double-check this:
- Open
File → Project Structure → Modules; - Select your main module, switch to the
Dependenciestab; - Locate the problematic JAR—its scope should be set to
Modulepath. If it's onClasspath, use the dropdown to switch it, then apply and save changes.
3. Manually Associate the Module (For Non-Build-Tool Projects)
If you're adding the JAR manually (no Maven/Gradle), make sure IntelliJ correctly maps it to the automatic module name:
- Go to
File → Project Structure → Libraries; - Right-click the JAR and select
Add to Modules; - In the popup, ensure your main module is selected. IntelliJ should auto-detect the
Automatic-Module-Namefrom the manifest, but if it doesn't, double-check that therequiresstatement in yourmodule-info.javaexactly matches the name in the manifest (module names are case-sensitive!).
4. Validate Build Tool Configurations (Maven/Gradle)
If you're using a build tool, confirm your setup is correct:
- Maven: No special config is needed—Maven automatically recognizes
Automatic-Module-Namefrom the JAR's manifest. Try right-clicking your project →Maven → Reload Projectto refresh dependencies. - Gradle: Ensure you're using
implementationorapifor the dependency, and that the Java plugin is enabled (Gradle 6+ supports modules by default). Rungradle clean buildfrom the terminal, then sync the project with IntelliJ.
5. Double-Check Module Name Spelling & Case
Last but not least, verify there's no typo:
- Open the JAR's
META-INF/MANIFEST.MFand copy the exact value ofAutomatic-Module-Name; - Compare it against the
requiresstatement in yourmodule-info.java—even a single uppercase/lowercase mismatch will cause IntelliJ to throw a fit.
Most of the time, either the cache clear or modulepath fix will resolve the issue. I fixed mine with the invalidate cache + resync combo, so start there!
内容的提问来源于stack exchange,提问作者Renato

