Java.lang.NoSuchFieldError exception when defining a field

I am working on a web application for Apache Tomcat. I use a class called "Location" in which I defined a field called ipAddresses. Here he is:

package com.maxmind.geoip; import java.util.HashSet; import java.util.Set; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; public class Location { private final static double EARTH_DIAMETER = 2 * 6378.2; private final static double PI = 3.14159265; private final static double RAD_CONVERT = PI / 180; public Set<String> ipAddresses; public String countryCode; public String countryName; public String region; public String city; public String postalCode; public float latitude; public float longitude; public int dma_code; public int area_code; public int metro_code; public Location() { ipAddresses = new HashSet<String>(); } ... } 

However, after deploying webapp on the server (war file) and trying to run a servlet that uses this class in it, I get a java.lang.NoSuchFieldError exception for IPaddresses .

Also, when trying to debug (Eclipse), when I reach the place where Location loc = new Location () is called, two strange things happen:

  • The encoded constructor is not called, the debugger will not enter it, instead the program counter arrow is displayed at the input in the Location.java file.
  • After “returning” from a call to Location loc = new Location () , when I look at the contents of the object, this field does not actually exist.
  • The source file that was deployed with the jar file includes this field.

I have tried many things:

  • cleaning and building the project and its redeployment.
  • cleaning the server’s working directory, either manually or using Eclipse.
  • Changing the server’s working directory in Eclipse.
  • Reinstalling the server in Eclipse.
  • completely reinstall Tomcat, three times and in different places!

I'm pretty stuck. What could it be?

+1
source share
2 answers

The symbols indicate that you have the class com.maxmind.geoip.Location in some JAR in JRE/lib or JRE/lib/ext or anywhere else in the path of the Eclipse / Tomcat class, which will always take precedence when loading classes by WAR classes.

Considering loc.getClass().getProtectionDomain().getCodeSource().getLocation() after creating it should give an idea of ​​where it is actually loaded.

+3
source

Are you using the box? Are there any links to it? It is possible that Eclipse is optimizing it.

In Windows->Preferences->Java->Compiler there is a setting: Preserve unused (never read) local variables . Is it verified? If not, check and try again.

0
source

All Articles