Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer

今天做了一个java对象转Map的例子,执行的时候报错了,如下:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
	at org.apache.commons.beanutils.BeanMap.createTypeTransformers(BeanMap.java:132)
	at org.apache.commons.beanutils.BeanMap.(BeanMap.java:68)
	at com.example2.pojo.PojoToMap.objectToMap(PojoToMap.java:23)
	at com.example2.pojo.PojoToMap.main(PojoToMap.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
	... 16 more

先贴上本例的主要代码吧:

package com.example2.pojo;

import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanMap;

public class PojoToMap {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Employee employee = new Employee();
		employee.setName("张三");
		employee.setAge(30);
		Map map;
		try {
			map = objectToMap(employee);
			Set> set = map.entrySet();
			for (Map.Entry entry : set) {
				System.out.println(entry.getKey() + " : " + entry.getValue());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Map objectToMap(Object obj) {
		if (obj == null)
			return null;
		return new BeanMap(obj);
	}
}

Employee类:

package com.example2.pojo;

public class Employee {

	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

从报错信息上看,是缺少了org.apache.commons.collections.Transformer类,网上下载了commons-collections-3.2.1.jar包,引入后,重新执行,成功。

 

 

 

你可能感兴趣的:(java,java对象转map,collections)