【问题标题】:Is there a way to use kotlin.random.Random inside GraalVM native image有没有办法在 GraalVM 原生镜像中使用 kotlin.random.Random
【发布时间】:2020-04-28 18:17:11
【问题描述】:

我尝试使用 Kotlin 构建一个简单的应用程序,该应用程序在 GraalVM 本机映像上使用 kotlin.random.Random 类。 但这在运行时失败。 Stacktrace 见下文。

作为一种解决方法,您可以使用 java 标准 java.util.Random 类。

谁能告诉我如何使用 Kotlin 类型?

App.kt

import kotlin.random.Random

fun main(args: Array<String>) {
   println(Random.nextInt())
}

Dockerfile

############################################################################
# Graal Native Image Early-Access Build
#
# Make sure you configured Docker to use at least 6gb ram!
# build from project root dir with: docker build -t example-kotlin-random-graalvm-native:1.0.0-SNAPSHOT .
# run with: docker run -d example-kotlin-random-graalvm-native:1.0.0-SNAPSHOT
############################################################################
#####
# The builder image to build the native app
#####
FROM findepi/graalvm:native as builder
LABEL stage=builder

WORKDIR /builder
COPY ./build/libs/app-all.jar ./app.jar
RUN native-image \
    --no-fallback \
    --static \
    -jar app.jar

#####
# The actual image to run
#####
FROM alpine:3.9
RUN apk --no-cache add ca-certificates

# App
WORKDIR /app
COPY --from=builder /builder/app .
EXPOSE $PORT
ENTRYPOINT ["./app"]

运行时错误

Exception in thread "main" java.lang.ExceptionInInitializerError

at com.oracle.svm.core.hub.ClassInitializationInfo.initialize(ClassInitializationInfo.java:290)

at java.lang.Class.ensureInitialized(DynamicHub.java:475)

at kotlin.random.Random.<clinit>(Random.kt:242)

at com.oracle.svm.core.hub.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:350)

at com.oracle.svm.core.hub.ClassInitializationInfo.initialize(ClassInitializationInfo.java:270)

at com.example.AppKt.main(App.kt:8)

Caused by: java.lang.InstantiationException: Type `kotlin.internal.jdk8.JDK8PlatformImplementations` can not be instantiated reflectively as it does not have a no-parameter constructor or the no-parameter constructor has not been added explicitly to the native image.

at java.lang.Class.newInstance(DynamicHub.java:793)

at kotlin.internal.PlatformImplementationsKt.<clinit>(PlatformImplementations.kt:41)

... 6 more

最小的工作示例项目here

【问题讨论】:

  • 我没有使用过 Kotlin,但我怀疑您需要在 native-image 调用中包含 Kotlin 库。
  • 看起来 kotlin 在那里使用反射,您可能必须使用 manual reflection configuration

标签: java kotlin graalvm graalvm-native-image


【解决方案1】:

你必须修改reflections rules
在 native-image 参数中添加以下内容:

-H:ReflectionConfigurationFiles=/path/to/reflectconfig

并将受影响类的规则放入反射配置文件中:

[{
    "name" : "kotlin.internal.jdk8.JDK8PlatformImplementations",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredFields" : true,
    "allPublicFields" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true
}]

或者你也可以在同一个文件read this中指定init方法

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 2021-09-09
    • 2020-09-09
    • 2021-01-07
    • 2021-02-03
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多