0

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
hnefatl
  • 5,860
  • 2
  • 27
  • 49

2 Answers2

0

Most probably error is in your manifest.mf

Manifest-Version: 1.0 Premain-Class: DriftDetector

Tty like this its works for me

Shehan Perera
  • 339
  • 1
  • 18
0

You can view some javaagent examples from https://github.com/kananindzya/javaagent-example .

In "memory-counter" module you can find information in README.md how to build and run manual or with maven and idea.