当前位置: 首页 > 知识库问答 >
问题:

突然无法通过VSCode启动配置运行Java[重复]

叶声
2023-03-14

我正在使用VSCode devcontainer编写一个Java应用程序。我把它放下大约一个月,回来工作它,现在我得到一些不熟悉的错误。

在这里,我将为devcontainer环境提供相关的配置文件。

我的Dockerfile如下:

FROM openjdk:16-slim-buster

# Install things
RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
    #
    # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
    && apt-get -y install curl git openssh-client vim less iproute2 procps lsb-release

# Install Maven CLI things
ARG MAVEN_VERSION=3.6.3
ARG MAVEN_SHA=c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && curl -fsSL -o /tmp/apache-maven.tar.gz https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  && echo "${MAVEN_SHA} /tmp/apache-maven.tar.gz" | sha512sum -c - \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG /root/.m2

# Allow for a consistant java home location for settings - image is changing over time
RUN if [ ! -d "/docker-java-home" ]; then ln -s "${JAVA_HOME}" /docker-java-home; fi
{
    "name": "Java Development",
    "dockerFile": "Dockerfile",

    // Set *default* container specific settings.json values on container create.
    "settings": { 
        "terminal.integrated.shell.linux": "/bin/bash",
        "java.home": "/docker-java-home"
    },
    
    // Add the IDs of extensions you want installed when the container is created.
    "extensions": [
        "vscjava.vscode-java-pack",
        "redhat.vscode-xml"
    ]
}

com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalStateException: Unable to load cache item
        at com.insurexcel.poipoi.input.InputProcessorTest.setup(InputProcessorTest.java:34)
Caused by: java.lang.IllegalStateException: Unable to load cache item
        at com.insurexcel.poipoi.input.InputProcessorTest.setup(InputProcessorTest.java:34)
Caused by: java.lang.ExceptionInInitializerError
        at com.insurexcel.poipoi.input.InputProcessorTest.setup(InputProcessorTest.java:34)
Caused by: com.google.inject.internal.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 @523fa30b
        at com.insurexcel.poipoi.input.InputProcessorTest.setup(InputProcessorTest.java:34)
Caused by: 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 @523fa30b
        at com.insurexcel.poipoi.input.InputProcessorTest.setup(InputProcessorTest.java:34)

所以看起来和某种反射有关。我使用的是GSON和Guice,以下是pom.xml的相关部分:

        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.2.3</version>
        </dependency>

我尝试将Guice更新到5.0.0-beta-1,这修复了测试中的错误,并成功编译。但是,现在我在执行某些序列化时会出现运行时错误。以下是错误的样子:

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private java.lang.String java.lang.Throwable.detailMessage accessible: module java.base does not "opens java.lang" to unnamed module @523fa30b
        at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
        at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
        at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177)
        at java.base/java.lang.reflect.Field.setAccessible(Field.java:171)
        at com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
        at com.google.gson.Gson.getAdapter(Gson.java:458)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
        at com.google.gson.Gson.getAdapter(Gson.java:458)
        at com.google.gson.Gson.toJson(Gson.java:696)
        at com.google.gson.Gson.toJson(Gson.java:683)
        at com.google.gson.Gson.toJson(Gson.java:638)
        at com.google.gson.Gson.toJson(Gson.java:618)
        ...

很明显这和GSON有关。它似乎无法访问私有字段?我试图序列化的对象如下所示:

public class MapContainer {
    private MyMapObject map;
    private String status;
    private Exception errorInformation;

    public MapContainer(MyMapObject map) {
        this.map = map;
        this.status = "okay";
        this.errorInformation = null;
    }
    public MapContainer(Exception error) {
        this.map = null;
        this.status = "error";
        this.errorInformation = error;
    }

    ...
    getters and setters
    ...
}

openjdk 16-ea 2021-03-16
OpenJDK Runtime Environment (build 16-ea+30-2130)
OpenJDK 64-Bit Server VM (build 16-ea+30-2130, mixed mode, sharing)

如果有人能提供任何线索或想法,这可能是从哪里来的,我将非常感谢!

共有1个答案

彭弘方
2023-03-14

问题是Gson试图使用反射访问java.lang模块中某个类的私有字段。

在Java9之前的日子里,这很好。使用Java9可以抛出异常。

快速而肮脏的解决方法是在java命令行中使用-add-opens选项。有关更多信息,请参见第一个参考。

有关这些选项的更多信息,请参见:

  • 如何在Java 9上解决InaccessibleObjectException(“无法使{member}可访问:模块{A}不‘打开{package}’到{B}”)?
  • GSON高级-用于简化的自定义序列化

我检查了Docker图像,我正在运行这个,它似乎最近已经更新了。

<罢工> 然而,对于这个问题已经“突然”开始发生 由于容器中的Java版本发生了变化,以前的版本必须是Java8或更早的版本。

在查看OpenJDK站点上的Java16页面时,我看到它正在实现JEP396:默认情况下强封装JDK内部,这将阻止Gson干扰私有字段的访问。如果你阅读了JEP,可能会有另一个解决方案。

 类似资料:
  • 我试图启动我的angular项目,但出于某种原因,每次我试图启动它时,当我试图通过终端中的VScode上的启动项目时,都会出现这个错误 我试图重新安装node和angular cli,但它仍然给出了这个错误,不知道是什么错了

  • 我试图在Intellij终极版15.0.4中配置Spring Boot运行配置。完成以下工作。 单击以创建新的Spring Boot配置 添加了主类、VM选项、JRE和环境变量 我无法在Spring Boot设置下找到启用启动优化和启用JMX代理?谁能推荐一下吗

  • 每当我通过命令行和mvn包运行应用程序时,IntelliJ 2016.1.2中都有一个小型Spring boot应用程序版本1.3.3 我记得我前段时间搜索过这个问题,它是IDE中一个已知的错误,也出现在Eclipse中。但是我找不到这些帖子了,因此无法访问解决方案。 你们谁能帮忙吗? 提前感谢。问候

  • 在“系统偏好设置”中,MySql连接状态只是在绿色和红色之间不断切换。当尝试在终端中开始时,我得到: 加洛夫楼下:~mgarofano$mysqld 2020-06-10T23:32:06.839640Z 0[系统][MY-010116][服务器] /usr/local/mysql-8.0.16-macos10.14-x86_64/bin/mysqld(mysqld 8.0.16)作为进程1353

  • 问题内容: 我无法通过暴发户来运行我的golang编。 这是我的项目目录- 我将我的gopath设置为上述目录。 在该文件夹中,我可以运行并且可以运行。 但是,通过以下新贵配置,其日志记录- 为什么它不能像我手动运行命令时那样从新贵开始工作? 问题答案: 摘自评论: 通过Upstart调用时,是否有可能未设置?尝试编译您的程序(),然后将其放到正常位置。