java - 为什么在 Linux 上使用 Java Attach API 会失败? (即使 Maven 构建完成)

标签 java exception maven tools.jar

我一直在使用 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构建使用配置文件选择位置,然后在打包阶段之前将jar预安装在本地repo中(允许依赖只是正常依赖)。

配置文件部分...

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

安装文件部分...

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

关于java - 为什么在 Linux 上使用 Java Attach API 会失败? (即使 Maven 构建完成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16379208/

相关文章:

javascript - 在 UIBinder 的 iFrame 中添加 GWT 小部件?

java - Jackson JSON 映射 Facebook 数据

java - 将 printStackTrace() 用于 caugt 异常是一个坏主意吗?

asp.net-mvc - 间歇性的asp.net mvc异常: “A public action method ABC could not be found on controller XYZ.”

java - 键盘按下和 Jbutton

java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 类型 com.test.consti.last.QuestionEntry

java - JDK8 CompletableFuture.supplyAsync如何处理interruptedException

maven - "java: package org.junit does not exist"即使它在 Maven pom 中

java - 在 Tomcat 上运行的 Jersey Servlet?

java - 将 spring-data-rest 添加到 gs-rest-service 示例项目后出现依赖问题