首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java新手:如何从命令行使用Java Agent进行指令插入

Java新手:如何从命令行使用Java Agent进行指令插入
EN

Stack Overflow用户
提问于 2015-02-25 09:14:59
回答 2查看 1.9K关注 0票数 0

我正在尝试加载一个java代理,以使用java.lang.instrument.Instrumentation来测量对象的大小。这是我的Java:

代码语言:javascript
复制
package com.example.memory.usage;

import java.lang.instrument.Instrumentation;
import java.util.*;


public class MemoryUsage {

    public static void main(String[] args) throws Exception {

        Random random = new Random();

        Set<Integer> integerSet = new HashSet<>();

        for(int i = 0; i < pixels; i++) {
            if(random.nextDouble() < 0.20) {
                integerSet.add(i);
            }
        }

        System.out.println(ObjectSizeFetcher.getObjectSize(integerSet));
    }

    public static class ObjectSizeFetcher {
        private static Instrumentation instrumentation;

        public static void premain(String args, Instrumentation inst) {
            System.out.println("Premain ... " + inst);
            instrumentation = inst;
        }

        public static long getObjectSize(Object o) {
            return instrumentation.getObjectSize(o);
        }
    }
}

由于这是一个Maven项目,下面是我的pom.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>memory-usage</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <finalName>memory-usage</finalName>
    <plugins>
        <!-- Compiler -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <!-- Jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.memory.usage.MemoryUsage</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <!-- Assembly -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.example.memory.usage.MemoryUsage</mainClass>
                            </manifest>
                            <manifestEntries>
                                <Premain-Class>java.lang.instrument.Instrumentation</Premain-Class>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

不,我不知道下一步该做什么。请帮帮忙。我跑了

代码语言:javascript
复制
$ mvn clean package

然后

代码语言:javascript
复制
$ java -jar target/memory-usage-jar-with-dependencies.jar

这给了我一个例外:

代码语言:javascript
复制
Exception in thread "main" java.lang.NullPointerException
    at com.example.memory.usage.MemoryUsage$ObjectSizeFetcher.getObjectSize(MemoryUsage.java:42)
    at com.example.memory.usage.MemoryUsage.main(MemoryUsage.java:29)

我应该怎么做才能运行这个?

EN

回答 2

Stack Overflow用户

发布于 2015-03-23 20:32:59

启动机制被认为是JVM/JRE特定的。但是,package-summary of the instrumentation API会告诉您:

命令行界面

实现不需要提供从命令行界面启动代理的方法。在提供了从命令行界面启动代理的方法的实现上,通过将以下选项添加到命令行来启动代理:

-javaagent:jarpath=options

jarpath是代理JAR文件的路径。options是座席选项。此开关可以在同一命令行中多次使用,从而创建多个代理。多个代理可以使用相同的jarpath。代理JAR文件必须符合JAR文件规范。

launcher documentation还提供了一个提示:

-javaagent:jarpath=options

加载指定的Java编程语言代理。有关检测Java应用程序的更多信息,请参见http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html上的Java API文档中的java.lang.instrument包说明

但请注意,您必须在清单中指定正确的代理类。据我所知,您将java.lang.instrument.Instrumentation指定为premain类,这是无稽之谈。在您的代码中,包含premain方法的类是com.example.memory.usage.MemoryUsage$ObjectSizeFetcher,因此您应该指定该类…

票数 1
EN

Stack Overflow用户

发布于 2015-02-25 09:21:44

您静态地调用getObjectSize。需要初始化变量指令插入(即instrumentation = new Instrumentation();)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28709455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档