为什么在linux上使用Java Attach API会失败?(即使maven构建完成)



我一直在使用Java Attach API (tools.jar的一部分)来附加到正在运行的Java进程,并从内部关闭它。

它在Windows上工作得很好。然而,当尝试在linux上运行时实际执行附加代码时,我得到一个java.lang.NoClassDefFoundError,其中包含以下堆栈跟踪原因…

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)

我正在使用Maven,到目前为止,我有这个部分,为了包含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>

值得注意的是${java。home}计算为jre,但即使我将其更改为直接指向jdk的路径,问题还是一样的。

原来这是maven构建的一个问题。系统作用域要求容器在启动时在类路径上传递tools.jar。简单的java -jar不会这样做(并且我不想添加显式的类路径参数)。

我把解决这个问题的解决方案放在一起是让maven构建使用配置文件选择位置,然后在打包阶段之前在本地repo中预安装jar(允许依赖关系只是正常的依赖关系)。

概要文件部分…

<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文件部分…

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

从属

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

最新更新