2

I need to use compiler tree api in my custom annotation processor logic . the compiler tree api belongs to sun tools.jar . i was thinking it could be import as a simple normal gradle dependency like import group: 'com.sun', name: 'tools', version: '1.8' , But it's not working .

I've searched many tech blogs , it seems like the common solution is compile file("${System.properties['java.home']}/../lib/tools.jar") , apparently it's lake of compatibility。 is there any better solution to solve this ?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Adams.H
  • 1,603
  • 4
  • 20
  • 29
  • Just to make sure, are you using Java 8? Because `tools.jar` no longer exists as of Java 9; its code was moved into JDK modules (e.g., [`jdk.compiler`](https://docs.oracle.com/en/java/javase/19/docs/api/jdk.compiler/module-summary.html)). – Slaw Dec 29 '22 at 08:12
  • yes,i'm using Java 8 . – Adams.H Dec 29 '22 at 08:14
  • 1
    Then my guess is what you've found is the best approach, though I've never personally had reason to depend on `tools.jar` when using Java 8. But I doubt it's a Maven artifact, as it's part of the implementation of a JDK. What you've found seems reasonable, at least for an application or sub-project library. I'm not sure how `file` dependencies work with external libraries (i.e., those published to and downloaded from a repository). You also have to be mindful of the fact that JREs typically don't include `tools.jar`. – Slaw Dec 29 '22 at 08:23
  • My process logic only run at compile time , so JRE is not a problem . it's quite reasonable because application won't depends on jdk things usually . but sometimes it's the only way to get things done , ie https://stackoverflow.com/questions/6373145/accessing-source-code-from-java-annotation-processor . – Adams.H Dec 29 '22 at 08:44

0 Answers0