Why is using the Java Attach API not working on Linux? (although maven build is complete)

I use the Java Attach API (part of tools.jar) to attach to a running Java process and close it internally.

It works great on Windows. However, when you try to execute attach code while working on linux, I get java.lang.NoClassDefFoundError with the following stack trace for the reason ...

 java.lang.ClassNotFoundException:com.sun.tools.attach.VirtualMachine... java.net.URLClassLoader$1.run(URLClassLoader.java:202) java.security.AccessController.doPrivileged(Native Method) java.net.URLClassLoader.findClass(URLClassLoader.java:190) java.lang.ClassLoader.loadClass(ClassLoader.java:306) sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

I am using Maven and so far I have this section to enable tools.jar.

 <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.4.2</version> <scope>system</scope> <systemPath>${java.home}/../lib/tools.jar</systemPath> </dependency> 

In particular, the value of $ {java.home} is evaluated in jre, but even if I change it to the direct path to jdk, the problem will be the same.

I am very confused ...

+4
source share
1 answer

Turns out it was a maven build issue. The system area requires the container to pass tools.jar to the classpath at startup. Simple java -jar does not do this (and I do not want to add an explicit classpath argument).

The solution I put together to solve this problem is for the maven assembly to select a location using profiles, and then pre-install the jar in the local repo before the package phase (letting the dependency just be a normal dependency).

SECTION OF PROFILES ...

 <profiles> <profile> <id>default-profile</id> <activation> <activeByDefault>true</activeByDefault> <file> <exists>${java.home}/../lib/tools.jar</exists> </file> </activation> <properties> <toolsjar>${java.home}/../lib/tools.jar</toolsjar> </properties> </profile> <profile> <id>osx_profile</id> <activation> <activeByDefault>false</activeByDefault> <os> <family>mac</family> </os> </activation> <properties> <toolsjar>${java.home}/../Classes/classes.jar</toolsjar> </properties> </profile> </profiles> 

FILE INSTALLATION SECTION ...

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <executions> <execution> <id>jdk_tools</id> <phase>prepare-package</phase> <goals> <goal>install-file</goal> </goals> <configuration> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.4.2</version> <packaging>jar</packaging> <file>${toolsjar}</file> </configuration> </execution> </executions> </plugin> 

dependencies

 <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.4.2</version> </dependency> 
+5
source

Source: https://habr.com/ru/post/979712/


All Articles