I am trying to run a basic java agent.
I followed this:
How to put classes for javaagent in the classpath
But I am getting these errors
Failed to find Premain-Class manifest attribute in target/demo.jar
Error occurred during initialization of VM
agent library failed to init: instrument
Here is my agent
package com.example.demo;
import java.lang.instrument.Instrumentation;
public class DriftDetector {
private static Instrumentation instrumentation;
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("Inside premain");
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
My manifest.mf (inside META-INF)
Manifest-Version: 1.0
Premain-Class: com.example.demo.DriftDetector
and my main class
package com.example.demo;
import static com.example.demo.DriftDetector.getObjectSize;
public class DemoApplication {
public static void main(String[] args) {
System.out.println("Size of Long: " + getObjectSize(new Long(1L)));
}
}
Command to create jar
mvn package
command to run
java -javaagent:target/demo.jar -jar target/demo.jar