'Trouble with minimal java agent

I can't seem to get a simple agent to work and it's frustrating me quite a bit...

Class upon which agent will act:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from main");
    }
}

Agent class:

package instrumentation;

public class Agent {
    public static void premain(String args, 
            Instrumentation inst) throws IOException {
        System.out.println("Hello from agent");
    }
}

MANIFEST.MF:

Premain-Class: instrumentation.Agent

JVM argument passed:

-javaagent:C:\path\to\agent.jar

The only output I get is:

Hello from main

What am I doing wrong?



Solution 1:[1]

Funny enough, the position at which you place your javaagent argument matters; It must come before the jar argument.

So try this:

java -javaagent:agent.jar -jar agent.jar

Assuming your java agent is packaged along with your source, of course.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FlashDaggerX