cglib bean复制报错:module java.base does not “opens java.lang“ to unnamed module

在使用cglib bean复制功能时,报下面的错误

Caused by: net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @4bd4bcd4

代码示例如下

public class BeanCopierExample {

@Getter
@Setter
public static class Source {
    private String name;
    private int age;
    private String[] logFieldOpsTypes;
}

@Setter
@Getter
public static class Target {
    private String name;
    private String[] logFieldOpsTypes;

    @Override
    public String toString(){
        return GsonUtils.toJson(this);
    }

}

    public static void main(String[] args) {
        String[] s = new String[]{"a","b","c"};
        Source source = new Source();
        source.setName("John Doe");
        source.setAge(30);
        source.setLogFieldOpsTypes(s);
        Target t= BeanTools.copyBean(source,Target.class);
        System.out.println(t.getName()); // prints "John Doe"
        System.out.println(t);
    }

}

增加vm环境变量,下面两种方式都可以把这个环境变量加上

--add-opens java.base/java.lang=ALL-UNNAMED
--add-exports java.base/jdk.internal.module=ALL-UNNAMED

idea的修改步骤如下,
点击 RUN-Edit Configurations,将上述内容添加到 VM options 文本框中,
cglib bean复制报错:module java.base does not “opens java.lang“ to unnamed module_第1张图片

如果没有 VM options 文本框,说明隐藏起来了,可以点击 Build and run的右边的 “Modify options”
cglib bean复制报错:module java.base does not “opens java.lang“ to unnamed module_第2张图片
如下图所示点击“Add VM options” 将其显示出来
cglib bean复制报错:module java.base does not “opens java.lang“ to unnamed module_第3张图片

你可能感兴趣的:(java,python,开发语言)