【发布时间】:2021-04-09 06:08:08
【问题描述】:
我正在使用 VSCode 开发容器来编写 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
我的开发者 JSON 在这里:
{
"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"
]
}
问题
当我之前停止使用此应用程序时,一切编译和运行都很好,我能够通过 VSCode 启动我的配置进行测试。现在,我遇到了两个问题,我认为它们是相关的。首先,当我尝试使用启动配置运行我的应用程序时,它不起作用并且我收到了这个奇怪的错误:
所以我无法通过 VSCode 启动任何东西。接下来,我尝试了mvn package,但我的测试失败并出现以下错误:
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
...
}
根据错误,我的对象中的异常字段似乎很难处理。我已经查看了所有内容,但无法真正找到可能发生这种情况的充分理由。我检查了正在运行它的 Docker 映像,它似乎最近已更新。我在容器中的java --version 返回这个:
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)
如果有人能提供任何线索或想法来说明这可能来自哪里,我将不胜感激!
【问题讨论】:
-
除非您知道自己在做什么,否则不应使用 EA 版本的 java。现在使用 java 15 或使用 java 11 直到 java 17 出来。