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

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 全书网Java爬取数据
  • ¥15 SAP HANA SQL Script 。SUM OVER 怎么加where
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥60 为什么使用python对地震数据进行umap降维后,数据成图会出现不连续的现象
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题