java.lang.reflect.InaccessibleObjectException: Unable to make

简介: java.lang.reflect.InaccessibleObjectException: Unable to make ...This exception occurs in a wide variety of scenarios when running an application on Java 9.

java.lang.reflect.InaccessibleObjectException: Unable to make ...

This exception occurs in a wide variety of scenarios when running an application on Java 9. Certain libraries and frameworks (Spring, Hibernate, JAXB) are particularly prone to it.

比如说,下面这段代码:

package com.guigarage.shell;

import java.util.List;

import com.alibaba.fastjson.JSON;

import jdk.jshell.JShell;
import jdk.jshell.SnippetEvent;

public class JShellTest {

    private static JShell shell;

    public static synchronized void eval(String command) {
        shell = JShell.builder().build();
        List<SnippetEvent> events = shell.eval(command);
        System.out.println(events);
        System.out.println(JSON.toJSONString(events));

    }

    public static void main(String[] args) {
        eval("System.out.println(\"Hello,World!\")");
    }

}


运行到JSON.toJSONString 就会报错了:

Hello,World!
[SnippetEvent(snippet=Snippet:StatementKey#1-System.out.println("Hello,World!"),previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=)]
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final boolean jdk.jshell.SnippetEvent.isSignatureChange accessible: module jdk.jshell does not "opens jdk.jshell" to unnamed module @2421cc4
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
    at com.alibaba.fastjson.util.TypeUtils.setAccessible(TypeUtils.java:1798)
    at com.alibaba.fastjson.util.FieldInfo.<init>(FieldInfo.java:165)
    at com.alibaba.fastjson.util.TypeUtils.computeGetters(TypeUtils.java:1498)
    at com.alibaba.fastjson.util.TypeUtils.buildBeanInfo(TypeUtils.java:1196)
    at com.alibaba.fastjson.serializer.SerializeConfig.createJavaBeanSerializer(SerializeConfig.java:109)
    at com.alibaba.fastjson.serializer.SerializeConfig.getObjectWriter(SerializeConfig.java:693)
    at com.alibaba.fastjson.serializer.SerializeConfig.getObjectWriter(SerializeConfig.java:405)
    at com.alibaba.fastjson.serializer.JSONSerializer.getObjectWriter(JSONSerializer.java:359)
    at com.alibaba.fastjson.serializer.ListSerializer.write(ListSerializer.java:129)
    at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:278)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:652)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:594)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:559)
    at com.guigarage.shell.JShellTest.eval(JShellTest.java:18)
    at com.guigarage.shell.JShellTest.main(JShellTest.java:23)


The exception is caused by the Java Platform Module System that was introduced in Java 9, particularly its implementation of strong encapsulation. It only allows access under certain conditions, the most prominent ones are:

the type has to be public
the owning package has to be exported
The same limitations are true for reflection, which the code causing the exception tried to use. More precisely the exception is caused by a call to setAccessible.

更多参见:https://stackoverflow.com/questions/41265266/how-to-solve-inaccessibleobjectexception-unable-to-make-member-accessible-m

相关文章
|
7月前
|
容器
Unable to create tempDir. java.io.tmpdir is set to /tmp
Unable to create tempDir. java.io.tmpdir is set to /tmp
313 1
|
应用服务中间件
快速解决idea启动项目报错:Unable to open debugger port(127.0.0.1:58950):java.net.SocketException“socket closed
快速解决idea启动项目报错:Unable to open debugger port(127.0.0.1:58950):java.net.SocketException“socket closed
1013 0
Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.
Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.
580 0
|
22天前
|
Java
Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed
Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed
32 0
|
4月前
|
Java Android开发
解决Android编译报错:Unable to make field private final java.lang.String java.io.File.path accessible
解决Android编译报错:Unable to make field private final java.lang.String java.io.File.path accessible
608 1
|
4月前
Unable to create tempDir. java.io.tmpdir is set to /tmp
Unable to create tempDir. java.io.tmpdir is set to /tmp
57 0
|
6月前
|
Linux
elasticsearch启动报错:unable to install syscall filter: java.lang.UnsupportedOperationException: seccomp
elasticsearch启动报错:unable to install syscall filter: java.lang.UnsupportedOperationException: seccomp
50 0
|
Java Spring
【Java】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
【Java】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
265 0
|
SQL 关系型数据库 MySQL
Hive报错:HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.me
Hive报错:HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.me
887 0
|
7月前
|
Java Linux iOS开发
8 种 Java- 内存溢出之五 -Unable to create new native thread
8 种 Java- 内存溢出之五 -Unable to create new native thread