如何修复 java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine in java 21

问题描述 投票:0回答:1

我有一个 clojure 项目,其中

deps.edn
使用库 clj-async-profiler

我在项目中的主要功能包含以下代码-

(ns com.myns.cli
  (:gen-class)
  (:require
   [clj-async-profiler.core :as profiler]
   [clj-async-profiler.ui :as profiler.ui]))

(set! *warn-on-reflection* true)

(defn -main [& args]
      (profiler/serve-ui 8074)
      (.addShutdownHook (Runtime/getRuntime) (Thread. ^Runnable profiler.ui/stop-server))
  ;;(run config-file))

java应用程序通过命令启动

java -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints --add-opens java.base/jdk.internal.misc=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/jdk.attach=ALL-UNNAMED -Djdk.attach.allowAttachSelf -cp myapp.jar 

  WARNING: package jdk.attach not in java.base
  Exception in thread "main" java.lang.ExceptionInInitializerError
      at java.base/java.lang.Class.forName0(Native Method)
      at java.base/java.lang.Class.forName(Unknown Source)
      at java.base/java.lang.Class.forName(Unknown Source)
      at clojure.lang.RT.classForName(RT.java:2209)
      at clojure.lang.RT.classForName(RT.java:2218)
      at clojure.lang.RT.loadClassForName(RT.java:2237)
      at clojure.lang.RT.load(RT.java:449)
      at clojure.lang.RT.load(RT.java:424)
      at clojure.core$load$fn__6908.invoke(core.clj:6162)
      at clojure.core$load.invokeStatic(core.clj:6161)
      at clojure.core$load.doInvoke(core.clj:6145)
      at clojure.lang.RestFn.invoke(RestFn.java:408)
      at clj_async_profiler.core__init.load(Unknown Source)
      at clj_async_profiler.core__init.<clinit>(Unknown Source)
      at java.base/java.lang.Class.forName0(Native Method)
      at java.base/java.lang.Class.forName(Unknown Source)
      at java.base/java.lang.Class.forName(Unknown Source)
      at clojure.lang.RT.classForName(RT.java:2209)
      at clojure.lang.RT.classForName(RT.java:2218)
      at clojure.lang.RT.loadClassForName(RT.java:2237)
      at clojure.lang.RT.load(RT.java:449)
      at clojure.lang.RT.load(RT.java:424)
      at clojure.core$load$fn__6908.invoke(core.clj:6162)
      at clojure.core$load.invokeStatic(core.clj:6161)
      at clojure.core$load.doInvoke(core.clj:6145)
      at clojure.lang.RestFn.invoke(RestFn.java:408)
      at clojure.core$load_one.invokeStatic(core.clj:5934)
      at clojure.core$load_one.invoke(core.clj:5929)
      at clojure.core$load_lib$fn__6850.invoke(core.clj:5976)
      at clojure.core$load_lib.invokeStatic(core.clj:5975)
      at clojure.core$load_lib.doInvoke(core.clj:5954)
      at clojure.lang.RestFn.applyTo(RestFn.java:142)
      at clojure.core$apply.invokeStatic(core.clj:669)
      at clojure.core$load_libs.invokeStatic(core.clj:6017)
      at clojure.core$load_libs.doInvoke(core.clj:6001)
      at clojure.lang.RestFn.applyTo(RestFn.java:137)
      at clojure.core$apply.invokeStatic(core.clj:669)
      at clojure.core$require.invokeStatic(core.clj:6039)
      at identity_check.cli$loading__6789__auto____149.invoke(cli.clj:1)
      at identity_check.cli__init.load(Unknown Source)
      at identity_check.cli__init.<clinit>(Unknown Source)
      at java.base/java.lang.Class.forName0(Native Method)
      at java.base/java.lang.Class.forName(Unknown Source)
      at java.base/java.lang.Class.forName(Unknown Source)
      at clojure.lang.RT.classForName(RT.java:2209)
      at clojure.lang.RT.classForName(RT.java:2218)
      at clojure.lang.RT.loadClassForName(RT.java:2237)
      at clojure.lang.RT.load(RT.java:449)
      at clojure.lang.RT.load(RT.java:424)
      at clojure.core$load$fn__6908.invoke(core.clj:6162)
      at clojure.core$load.invokeStatic(core.clj:6161)
      at clojure.core$load.doInvoke(core.clj:6145)
      at clojure.lang.RestFn.invoke(RestFn.java:408)
      at clojure.lang.Var.invoke(Var.java:384)
      at clojure.lang.Util.loadWithClass(Util.java:251)
      at com.myns.cli.<clinit>(Unknown Source)
  Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
      at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
      at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
      at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
      at java.base/java.lang.Class.forName0(Native Method)
      at java.base/java.lang.Class.forName(Unknown Source)
      at java.base/java.lang.Class.forName(Unknown Source)
      at clojure.lang.RT.classForName(RT.java:2209)
      at clojure.lang.RT.classForNameNonLoading(RT.java:2222)
      at clj_async_profiler.util.jdk11__init.load(Unknown Source)
      at clj_async_profiler.util.jdk11__init.<clinit>(Unknown Source)
      ... 56 more
  

如何修复此错误? 我可以看到 jdk.attach 模块属于 java documentation 中的 java.base。为什么我在控制台中收到警告

WARNING: package jdk.attach not in java.base

注:

以下是java版本详细信息 -

java --version
openjdk 21.0.3 2024-04-16 LTS
OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode, sharing)

另外

--list-modules
命令返回 -

$java --list-modules
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

另外,我使用 docker image

eclipse-temurin:21-jre-jammy
如果有帮助的话

java clojure java-module java-platform-module-system async-profiler
1个回答
0
投票

问题确实出在 Docker 镜像上

eclipse-temurin:21-jre-jammy
。 clj-async-profiler 需要 JDK 才能工作。使用例如
eclipse-temurin:21-jdk-noble
应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.