0

I need to work with com.sun.tools.javac classes that are private and are not visible neither during compile nor run time.

I use:

  • JDK 11.0.15
  • Maven build tool
  • Intellij IDEA

My current state is that my imports are red-highlghited and compilation fails.

My class I want to use sun tools inside (sorry for the pic instead of code, my class is 2000+ lines length, for now I only care about availability of tools in my class):

enter image description here

pom.xml:

...

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I need to be able to have these classes available as I "type", during mvn compile and in runtime.

com.sun packages I'd like to have:

  • com.sun.tools.javac.code
  • com.sun.tools.javac.comp
  • com.sun.tools.javac.file
  • com.sun.tools.javac.main
  • com.sun.tools.javac.model
  • com.sun.tools.javac.parser
  • com.sun.tools.javac.processing
  • com.sun.tools.javac.tree
  • com.sun.tools.javac.util

Thank you in advance!

Serhii Kachan
  • 345
  • 4
  • 13

2 Answers2

1

I was able to make it work with the following compiler plugin configuration.

<compilerArgs>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
                    <arg>--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>

                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
                    <arg>--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
                </compilerArgs>

And other thing that broke my code was maven-javadoc-plugin added to my plugins.

Serhii Kachan
  • 345
  • 4
  • 13
0

The Javadoc for package com.sun.tools.javac in Java 11 says:

This package provides a legacy entry point for the javac tool. See the jdk.compiler module for details on replacement APIs.

Access the module java.compiler, package javax.tools, for the interface JavaCompiler and class ToolProvider.

See the Javadoc for example code.

The com.sun package was never standard, never supported, and never intended for external use.

See JEP 403: Strongly Encapsulate JDK Internals.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154