JAR包中生成的ENUM类无法访问,求排查可能原因
It’s frustrating when most classes in your JAR work cross-module but one enum doesn’t—let’s walk through the most probable causes and fixes:
Package-private access level
The #1 culprit here is usually missing thepublicmodifier on your enum. If it’s declared asenum YourEnum(instead ofpublic enum YourEnum), it’s package-private by default. That means it can only be accessed within its own package, so other modules (which are in different packages) can’t see it. Check the generated enum’s source code to confirm the access modifier.Missing enum .class file in the JAR
Yourjar tfoutput shows the directory structure, but it doesn’t explicitly list the enum’s compiled.classfile. Runjar tf your-project.jar | grep YourEnum(replace with your actual enum name) to verify if the file exists. If it’s missing, your build process might not be compiling the generated enum or including it in the JAR. Check your build script (Maven, Gradle, etc.) to ensure the generated Java file is part of the compilation and packaging steps.Java Module System (Java 9+) restrictions
If you’re using modules (withmodule-info.java), you need to explicitly export the package containing the enum from its module. Addexports com.processing.commonutils.enumGenerator;to themodule-info.javaof the module where the enum lives. Also, make sure the dependent module has arequires your-module-name;declaration to pull in the module with the enum. Without these, even public enums are blocked between modules.Classloader mismatch
This is less common, but possible in complex setups like application servers. If the module containing the JAR and the dependent module are loaded by different classloaders, the enum class might not be visible across them. To debug, print the classloader for a working class and compare it with the enum’s (if you can reference it) usingWorkingClass.class.getClassLoader()andYourEnum.class.getClassLoader().Typos or package name discrepancies
Double-check that the package name you’re using to import the enum matches exactly what’s in the JAR. A tiny typo (like lowercaseenumgeneratorinstead ofenumGenerator) will cause the JVM to fail locating the class, even if it’s present in the JAR.
内容的提问来源于stack exchange,提问作者priyas

