STU8080 2022-03-23 13:50 采纳率: 100%
浏览 51
已结题

Eclipse使用BeanUtils框架,使用BeanUtils.setProperty时提示java.lang.IllegalAccessException

Eclipse使用BeanUtils框架,给属性赋值,已导入commons-beanutils和commons-logging的jar包,使用BeanUtils.setProperty时提示java.lang.IllegalAccessException

img

Person类:

package day_220322;

public class Person {

public String name;
private String password;
private int age;

public Person() {
    
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}

BeanDemo类:

package day_220322;

import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

public class BeanDemo {

@Test
public void test1() throws IllegalAccessException, InvocationTargetException  {
    
    Person p =new Person();
    BeanUtils.setProperty(p, "name", "xcc");

// BeanUtils.setProperty(p, "age", 18);
System.out.println(p.getName());
}
}

运行Run as Junit时,提示:

java.lang.IllegalAccessException: class org.apache.commons.beanutils.PropertyUtilsBean (in module commons.beanutils) cannot access class day_220322.Person (in module JavaStudy) because module JavaStudy does not export day_220322 to module commons.beanutils
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:385)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:693)
at java.base/java.lang.reflect.Method.invoke(Method.java:556)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2170)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2151)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1957)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2064)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1017)
at commons.beanutils@1.8.3/org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
at JavaStudy/day_220322.BeanDemo.test1(BeanDemo.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at junit@1500/org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at junit@1500/org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at junit@1500/org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at junit@1500/org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at junit@1500/org.junit.runners.ParentRunner$3.evaluat

  • 写回答

2条回答 默认 最新

  • qq_25376897 2022-03-23 14:16
    关注

    JavaStudy里面加一句 exports day_220322;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月31日
  • 已采纳回答 3月23日
  • 创建了问题 3月23日

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败