title: Java Newbie: How to use Java Agent From the Command Line for Instrumentation
body: <p>I am attempting to load a java agent to use <code>java.lang.instrument.Instrumentation</code> to measure the size of an object.  Here is my Java:</p>
  
  <pre><code>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&lt;Integer&gt; integerSet = new HashSet&lt;&gt;();
  
          for(int i = 0; i &lt; pixels; i++) {
              if(random.nextDouble() &lt; 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);
          }
      }
  }
  </code></pre>
  
  <p>As this is a Maven project, here is my <code>pom.xml</code></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
  &lt;groupId&gt;com.example&lt;/groupId&gt;
  &lt;artifactId&gt;memory-usage&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
  &lt;build&gt;
      &lt;finalName&gt;memory-usage&lt;/finalName&gt;
      &lt;plugins&gt;
          &lt;!-- Compiler --&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;version&gt;3.0&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;source&gt;1.7&lt;/source&gt;
                  &lt;target&gt;1.7&lt;/target&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  
          &lt;!-- Jar --&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.4&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;archive&gt;
                      &lt;manifest&gt;
                          &lt;mainClass&gt;com.example.memory.usage.MemoryUsage&lt;/mainClass&gt;
                      &lt;/manifest&gt;
                  &lt;/archive&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  
          &lt;!-- Assembly --&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;attached&lt;/goal&gt;
                      &lt;/goals&gt;
                      &lt;phase&gt;package&lt;/phase&gt;
                      &lt;configuration&gt;
                          &lt;descriptorRefs&gt;
                              &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                          &lt;/descriptorRefs&gt;
                          &lt;archive&gt;
                              &lt;manifest&gt;
                                  &lt;mainClass&gt;com.example.memory.usage.MemoryUsage&lt;/mainClass&gt;
                              &lt;/manifest&gt;
                              &lt;manifestEntries&gt;
                                  &lt;Premain-Class&gt;java.lang.instrument.Instrumentation&lt;/Premain-Class&gt;
                              &lt;/manifestEntries&gt;
                          &lt;/archive&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>No I do not know what to do next.  Please help.  I ran</p>
  
  <pre><code>$ mvn clean package
  </code></pre>
  
  <p>and then </p>
  
  <pre><code>$ java -jar target/memory-usage-jar-with-dependencies.jar
  </code></pre>
  
  <p>which gives me this exception:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>What should I do to run this?</p>