简体   繁体   中英

Why do I get a java.lang.NoSuchFieldError: on an Enum value?

I have an Enum inside a jar that I have produced myself. This jar is a dependency of a second jar, which uses the enum values.

Now, the second jar is a logging framework, whereas the first jar in this case is the model classes of the logging framework.

I am trying to implement this logging framework into a web application that I have made. Long story short, it still needs some work, but I am stuck on a single problem. An error in the framework's configuration initialization is caught as an exception, and it calls a method. This method has an Enum value as one of it's parameters. However, I get a java.lang.NoSuchFieldError on this enum.

The Enum value was ERROR, so i figured it could be a coincidence. But when I changed it to BABYLOVE the error message changed as well.

I've checked for redundancies and/or possible overlappings in class/enum names, but there are none that I can find.

Sequence of order:

  1. Web App calls for initialization of logging-framework (direct dependency)
  2. logging-framework has issues loading it's own configuration, and throws an exception
  3. Exception is handeled, and a method is called to register the error
  4. The method is called with several parameters, one which is an enum value from logging-framework-model.jar, which is a transitive dependency of the web app
  5. The web-app throws an exception

     java.lang.NoSuchFieldError: BABYLOVE at logging.framework.Constants.<clinit>(Constants.java:52) at logging.framework.Logger.<init>(Logger.java:60) at logging.framework.LogContext.getLoggerFromContext(LogContext.java:95) at logging.framework.LogContext.getCurrent(LogContext.java:48) at action.navigation.CalendarElementEditorAction.execute(CalendarElementEditorAction.java:39) Truncated. see log file for complete stacktrace 

Constants, line 51-52:

public static final Event ConfigValidationFailed = 
EventLogHelper.getEvent(EventLogSource.LoggingFramework, EventLogEntryType.BABYLOVE");

EventLogEntryType:

@XmlType(name = "EventLogEntryType")
@XmlEnum
public enum EventLogEntryType {

//for test purposes, should be removed. This variable is given a name that can not be confused with standard names in error messages, like Error and Warning can.
@XmlEnumValue("BabyLove")
BABYLOVE("BabyLove"),

@XmlEnumValue("Error")
ERROR("Error"),
@XmlEnumValue("Warning")
WARNING("Warning"),
@XmlEnumValue("Information")
INFORMATION("Information"),
@XmlEnumValue("SuccessAudit")
SUCCESSAUDIT("SuccessAudit"),
@XmlEnumValue("FailureAudit")
FAILUREAUDIT("FailureAudit");



private final String value;

EventLogEntryType(String v) {
    value = v;
}

public String value() {
    return value;
}

public static EventLogEntryType  fromValue(String v) {
    for (EventLogEntryType c: EventLogEntryType .values()) {
        if (c.value.equals(v)) {
            return c;
        }
    }
    throw new IllegalArgumentException(v);
}

I don't know if it matters, but I am using maven2 to deal with my dependencies.

I was told to check if the versions of my dependencies had mismatches, and after checking the war's content, I found that to be the problem.

My webapp is one of two very similar ones, that both has a dependency to a jar containing some base model and business logic classes. I had previously added the logging framework (version 1) to that project's pom.xml. So the logging framework 1.0 was a transitive dependency of the web app, while the logging framework 2.0 was a direct dependency of the web app. I am guessing that direct dependencies has precedence over transitive dependencies, so 2.0 was the one who was packaged into my war. However, since the logging framework is composed of a framework (direct dependency), and a set of model classes (transitive dependency), the war was packaged with logging framework model version 1.0.

After I unpacked the war, and found this, it was a pretty easy process to find out where it was wrongly imported, and I ended up with only logging framework version 2.0 for the complete set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM