定义字段时java.lang.NoSuchFieldError异常 [英] java.lang.NoSuchFieldError exception while the field is defined

查看:173
本文介绍了定义字段时java.lang.NoSuchFieldError异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Tomcat的Web应用程序。
我正在使用一个名为Location的类,其中我定义了一个名为ipAddresses的字段。这里是:

  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>();
}
...
}

但部署后webapp到服务器(war文件)并尝试运行正在使用此类的servlet,我得到 ipAddresses 的异常 java.lang.NoSuchFieldError



此外,当尝试调试(Eclipse)时,当我到达一个地方,其中位置loc = new Location()被调用时,发生两个奇怪的事情:


  1. 我编码的构造函数不被调用,调试器不会进入它,而是程序计数器箭头在Location.java文件中的导入中显示。

  2. 位置loc = new Location()调用返回后,当我'/'>
  3. 使用该jar文件部署的源文件包含此字段。

我尝试了很多东西:





  • 清理服务器的工作目录,手册和使用Eclipse。

  • 更改工作在Eclipse中的服务器目录。

  • 在Eclipse中重新安装服务器。

  • 完全重新安装Tomcat,三次并转到不同的位置!



我很卡住这可能是什么?

解决方案

症状表明您有 com.maxmind.geoip.Location在 JRE / lib JRE / lib / ext 中的某些JAR中的类其他Eclipse / Tomcat独立的类路径位置在WAR类的类加载中将始终具有优先级。



检查 loc.getClass()。getProtectionDomain ().getCodeSource()。getLocation()在构造之后,应该提供有关从哪里加载的实际的详细信息。


I'm working on a web application for Apache Tomcat. I'm using a class called Location in which i defined a field called ipAddresses. Here it 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 the webapp to the server (war file) and trying to run the servlet that is using this class in it, i'm getting the exception java.lang.NoSuchFieldError for ipAddresses.

Moreover, when trying to debug it (Eclipse), when i reach a place where Location loc = new Location() is called, two weird things happen:

  1. The constructor i coded isn't called, the debugger won't step into it, instead the program counter arrow is shown on on of the imports of in the Location.java file.
  2. After "returning" from the Location loc = new Location() call, when i'm viewing the object's content the field actually does not exist.
  3. The source file that was deployed with the jar file does include this field.

I have tried many things:

  • cleaning and building the project and redeploying it.
  • cleaning the server's working directory, both manual and by using Eclipse.
  • changing the working directory of the server, in Eclipse.
  • re-installing the server in Eclipse.
  • re-installing Tomcat entirely, three times and to different locations!

I'm pretty stuck. What could it be?

解决方案

The symptoms indicate that you have the com.maxmind.geoip.Location class in some JAR in the JRE/lib or JRE/lib/ext or any other Eclipse/Tomcat-independent classpath location which will always have precedence in classloading over the WAR's classes.

Examining the loc.getClass().getProtectionDomain().getCodeSource().getLocation() after you construct it should give insights about where it is actually been loaded from.

这篇关于定义字段时java.lang.NoSuchFieldError异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆