json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError

时间:2022-10-27 07:42:20
初学json,今天做了一个简单地json测试,代码很简单: 1.一个简单地Person类:
package com.anranxinghai.cloud.domain;

public class Person {
private int id;
private String name;
private String address;
public Person(){

}
public Person(int id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", address=" + address
+ "]";
}


}
2.一个简单地JsonService类,用来生成一个固定的Person(这好像是某种设计模式,知道的大神和我说下哈,小弟不才:
package com.anranxinghai.cloud.services;

import com.anranxinghai.cloud.domain.Person;

public class JsonService {

public JsonService() {
// TODO Auto-generated constructor stub
}

public Person getPerson(){
Person person = new Person(1,"anranxinghai","bejing");
return person;
}
}

3.一个简单地Tools类JsonTools:
package com.anranxinghai.cloud.tools;

import net.sf.json.JSONObject;


public class JsonTools {

public JsonTools() {

}

/**
*
* @param key
* 表示json字符串头信息
* @param object
* 是对解析的集合的类型
* @return object.toString
*/
public static String createJsonString(String key, Object object) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, object);

return jsonObject.toString();

}

}

4.测试类(为了错误明显,使用mian测试):
package com.anranxinghai.cloud.test;

import com.anranxinghai.cloud.domain.Person;
import com.anranxinghai.cloud.services.JsonService;
import com.anranxinghai.cloud.tools.JsonTools;

public class TestJson {

public static void main(String[] args) {
String msg = "";
JsonService jsonService = new JsonService();
Person person = jsonService.getPerson();
msg = JsonTools.createJsonString("persion", person);
System.out.println(msg);
}
}
结果:
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError 点击错误后: json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
奇怪,为什么这句话会出错,看来可能是源码出错,构造函数出错,向源码追溯: json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError 看来是
import org.apache.commons.collections.map.ListOrderedMap;
new ListOrderedMap();
的错误,在此处按下F3,发出警告声,证明导入的包中不存在这个类,查找org.apache.commons.collections开头的包: json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
果然不存在。 看来是没导入正确版本的jar包:

commons-collections-2.1.1.jar版本太低,找一个高版本的替换后:

可是换了之后仍然出现上述错误,百度,好多人这样说: json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
我就发现我的commons-lang-2.0.jar 里的包结构不同: json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
最终出现正确结果: json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
json 测试报错:Exception in thread "main" java.lang.NoClassDefFoundError
总结一下,在做工程之前,一定要将正确的包导入,而且版本不能错,以后调试错误要细心,否则事倍功半。 整个工程中有上述两处错误。 注:我试着把包commons-collections-2.1.1.jar换回去: 报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
目前我终于知道:导致错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException 的为lang包,而且人家说了,找不到NestableRuntimeException类,我却没有仔细看,如果我解决了这个问题,则下面就会报这个错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap即由commons-collections-2.1.1.jar包中没有ListOrderedMap类而报的错。