exception - Why does using the Java Attach API fail on linux? (even though maven build completes) -
i've been using java attach api (part of tools.jar) attach running java process, , shut down within.
it works on windows. when trying execute attach code when running on linux java.lang.noclassdeffounderror
following stack trace cause...
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'm using maven , far have section, in order include 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>
notably ${java.home} evaluates jre if change direct path jdk, issue same.
i'm pretty stumped...
turns out issue maven build. system scope requires container pass tools.jar on classpath @ launch. simple java -jar
not (and don't want add explicit classpath argument).
the solution put solve issue have maven build choose location using profiles, pre-install jar in local repo before package phase (allowing dependancy normal dependancy).
profiles section...
<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>
install-file 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>
dependancy
<dependency> <groupid>com.sun</groupid> <artifactid>tools</artifactid> <version>1.4.2</version> </dependency>
Comments
Post a Comment