user51
user51

Reputation: 10243

How to fix java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine in java 21

I've a clojure project with deps.edn which uses the library clj-async-profiler

My main function in the project contains the below code -

(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))

The java application is started with the command 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
  

How can I fix this error? I can see jdk.attach module belongs to java.base in java documentation. Why I get the warning WARNING: package jdk.attach not in java.base in the console ?

Note:

Below are the java version details -

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)

Also --list-modules commands returns -

$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]

Also, I use docker image eclipse-temurin:21-jre-jammy if it helps

Upvotes: 2

Views: 265

Answers (1)

Alexander Yakushev
Alexander Yakushev

Reputation: 75

The problem is indeed the Docker image eclipse-temurin:21-jre-jammy. clj-async-profiler requires JDK to work. Using e.g. eclipse-temurin:21-jdk-noble should work.

Upvotes: 0

Related Questions