A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 苏伯亚 中级黑马   /  2014-3-31 15:27  /  1335 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

现在beanutils更新到了1.9.1版本,下载下来后,也导入了,然后Commons Logging包也下载下来了,也导入了,怎么运行还是报错。错误代码如下

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.apache.commons.beanutils.ConvertUtilsBean.<init>(ConvertUtilsBean.java:154)
        at org.apache.commons.beanutils.BeanUtilsBean.<init>(BeanUtilsBean.java:112)
        at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:63)
        at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:59)
        at org.apache.commons.beanutils.ContextClassLoaderLocal.get(ContextClassLoaderLocal.java:154)
        at org.apache.commons.beanutils.BeanUtilsBean.getInstance(BeanUtilsBean.java:75)
        at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:380)
        at cn.itcast.day1.IntroSpectorTest.main(IntroSpectorTest.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 8 more

我的代码如下;
package cn.itcast.day1;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.BeanUtils;

public class IntroSpectorTest {
        public static void main(String[] args)throws Exception{
                ReflectPoint pt1=new ReflectPoint(3,5);
               
                String propertyName="x";
               
                PropertyDescriptor pd = getProperty(pt1, propertyName);
               
               
                //PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
                Object value=7;
               
                setProperties(pt1, pd, value);
               
                System.out.println(BeanUtils.getProperty(pt1, "x"));
               
        }

        private static void setProperties(Object pt1, PropertyDescriptor pd,
                        Object value) throws IllegalAccessException,
                        InvocationTargetException {
                Method methodSetX=pd.getWriteMethod();
                methodSetX.invoke(pt1,value);
                //Object retVal=methodGetX.invoke(pt1);
                //System.out.println(pt1.getX());
        }

        private static PropertyDescriptor getProperty(Object pt1,
                        String propertyName) throws IntrospectionException,
                        IllegalAccessException, InvocationTargetException {
                PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
                Method methodGetX=pd.getReadMethod();
                Object retVal=methodGetX.invoke(pt1);
                System.out.println(retVal);
                return pd;
        }

}
求指点,弄好久了。在这卡住了。

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

1 个回复

倒序浏览
我感觉你没有把jar包正确的导入进来,我测试过你的代码了,可以运行
  1. import java.beans.IntrospectionException;
  2. import java.beans.PropertyDescriptor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;

  5. import org.apache.commons.beanutils.BeanUtils;

  6. public class IntroSpectorTest {
  7.         public static void main(String[] args)throws Exception{
  8.                 ReflectPoint pt1=new ReflectPoint(3,5);
  9.                
  10.                 String propertyName="x";
  11.                
  12.                 PropertyDescriptor pd = getProperty(pt1, propertyName);
  13.                
  14.                
  15.                 //PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
  16.                 Object value=7;
  17.                
  18.                 setProperties(pt1, pd, value);
  19.                
  20.                 System.out.println(BeanUtils.getProperty(pt1, "x"));
  21.                
  22.         }

  23.         private static void setProperties(Object pt1, PropertyDescriptor pd,
  24.                         Object value) throws IllegalAccessException,
  25.                         InvocationTargetException {
  26.                 Method methodSetX=pd.getWriteMethod();
  27.                 methodSetX.invoke(pt1,value);
  28.                 //Object retVal=methodGetX.invoke(pt1);
  29.                 //System.out.println(pt1.getX());
  30.         }

  31.         private static PropertyDescriptor getProperty(Object pt1,
  32.                         String propertyName) throws IntrospectionException,
  33.                         IllegalAccessException, InvocationTargetException {
  34.                 PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
  35.                 Method methodGetX=pd.getReadMethod();
  36.                 Object retVal=methodGetX.invoke(pt1);
  37.                 System.out.println(retVal);
  38.                 return pd;
  39.         }

  40. }[code]
  41. public class ReflectPoint {
  42.         private int x;
  43.         private int y;
  44.         public int getX() {
  45.                 return x;
  46.         }
  47.         public void setX(int x) {
  48.                 this.x = x;
  49.         }
  50.         public int getY() {
  51.                 return y;
  52.         }
  53.         public void setY(int y) {
  54.                 this.y = y;
  55.         }
  56.         public ReflectPoint(int x, int y) {
  57.                 super();
  58.                 this.x = x;
  59.                 this.y = y;
  60.         }
  61.         public ReflectPoint()
  62.         {}

  63. }
复制代码
[/code]

254.png (5.92 KB, 下载次数: 7)

254.png
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马