'Command line load-time weaving

So I am starting with AspectJ and have a problem. The following example works perfectly in Eclipse but when I try to run it in bash - it does not work.

This is my code I will monitor:

public class TestClass {
  public static void method1() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    method1();
  }
}

This is my Aspect class:

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

    @Aspect
    public class LogMe {
        @Pointcut("call(public* TestClass.*())")
        public void metCall() {}

        @After("metCall()")
        public void aftercallAdvice() {
            System.out.println("Blah");
        }
    }

What I do is create jar called 'apsects.jar' with META-INF/aop-ajc.xml with

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
        <aspects>
                <aspect name="LogMe"/>
        </aspects>
        <weaver options="-showWeaveInfo"/>
</aspectj>

I try to run it like that:

java -javaagent:/home/tdi/dev/test/aspectjweaver.jar -classpath "aspects.jar;/home/tdi/dev/test/aspectjrt.jar" TestClass

What I get is:

Error: Could not find or load main class TestClass

How can I run this application with LTW aspects ?



Solution 1:[1]

Maybe it could be a problem that TestClass is not in the JAR-file together with the aspects. You could try to put a folder META-INF with your aop-ajc.xml into the working directory with TestClass.class.

Another option would be to compare your run command with that of eclipse. You can do this be running the project in Debug mode, then right click on the process and open its Properties. Then you can see how eclipse is running the project on the command line.

Solution 2:[2]

I ran into same issue, then I run below command follow the steps in this url flylib.com , then it worked

java -javaagent:"/Users/XXX/test/aspectj/lib/aspectjweaver-1.9.6.jar" -classpath .:lib/aspectjrt-1.8.13.jar com.XXX.aops.AOP5.HelloWorld

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 Thorben
Solution 2 LizzyMM