1

I have a jar with a main class.
I execute it with command java -jar my.jar
This main jar depends on another.jar (e.g. joda-time.jar).

Now I want to intercept a method of another.jar and say I want to print log.
By the way, I want to use my.jar as usual, I mean I'll call it as always: java -jar my.jar.

I have found a very nice example on github about external library weaving.
This example, intercepts a method of joda time library.
It has an aspect for toString() method of joda time.
However, it uses a unit test to intercept and demonstrate.

I've compiled and packed the given example as my_aspect.jar.
After that, I've moved my_aspect.jar to the execution directory, which contains my.jar, joda-time.jar.
Finally, I've also added aspectjrt.jar, and aspectjweaver.jar to the same directory.

When I call java -jar my.jar, intercepiton doesn't happen.
I think I have to tell the my_aspect.jar something to intercept but I don't know what to say.

Below is the main class of my.jar.
It simply makes a call to intercepted method.

package com.github.example;
import org.joda.time.DateTime;

public class Main {
    public static void main(String[] args) {
        System.out.println(new DateTime().toString());
    }
}
Ricardo Cristian Ramirez
  • 1,194
  • 3
  • 20
  • 42

1 Answers1

3

Both aspectjrt.jar and my_aspect.jar need to be on the class path if you use compile time weaving or binary weaving.

For load-time weaving you need to use java -javaagent:/path/to/aspectjweaver.jar -cp my_aspect.jar -jar my.jar. The project you use as a template also does it like that, see here.

If you don't want to use the Java agent and also don't want any AspectJ reference on the Java command line, then your only option is binary weaving for another.jar, creating a new version of it, and then creating an uber JAR (fat JAR), zipping up your whole application including dependencies and AspectJ runtime using a tool like One-JAR (there is also a Maven plugin for it).

Your question is lacking detail, so I cannot answer more precisely.


Update: Because you said you prefer the OneJar solution, you can search for my related posts. The group ID in those posts org.dstovall is outdated, though. That version was out of maintenance long ago, so I switched to the com.jolira fork, using it e.g. like this for AspectJ-enabled applications, of course in combination with AspectJ Maven plugin:

<plugin>
  <groupId>com.jolira</groupId>
  <artifactId>onejar-maven-plugin</artifactId>
  <version>1.4.4</version>
  <executions>
    <execution>
      <goals>
        <goal>one-jar</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <onejarVersion>0.96</onejarVersion>
    <mainClass>de.scrum_master.app.FooBar</mainClass>
    <attachToBuild>true</attachToBuild>
  </configuration>
</plugin>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • You've already answered precisely, thank you. I've missed, actually learnt, -javaagent part. Since I don't want to modify command that I use to execute, I'm gonna prefer über jar version. – Ricardo Cristian Ramirez Jul 03 '20 at 01:50
  • Happy to help. I updated the answer with some info about OneJar + Maven. Of course you can also create uber JARs with Maven Shade if you prefer to unpack the dependencies into your uber JAR instead of packaging them as sub-JARs. – kriegaex Jul 03 '20 at 02:23