简体   繁体   中英

How to run ajc from Java?

I'm trying to run ajc compiler from Java (not from Maven or Ant!). The question is which Maven dependency do I need and which class is an entry point? The best option I have now is org.aspectj.tools.ajc.Main from org.aspectj:aspectjtools:1.7.2 . Am I right?

Yes. In your Java project you need aspectjrt.jar (for the runtime) and aspectjtools.jar (for the compiler) on the class path. Then you can build an AspectJ project and create a JAR file containing aspects and classes like this:

import org.aspectj.tools.ajc.Main;

public class AjcRunner {
    public static void main(String[] args) throws Exception {
        String[] ajcArgs = {
            "-sourceroots", "c:\\my\\aspectj_project\\src",
            "-outjar", "my_aspects.jar"
        };
        Main.main(ajcArgs);
    }
}

Afterwards you can test the result on the console like this, assuming you have a class Application with a main method:

java -cp C:\path\to\aspectjrt.jar;my_aspects.jar Application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM