包com.sun.tools.javac.code在JDK17中不可见。

huangapple go评论76阅读模式
英文:

package com.sun.tools.javac.code is not visible in JDK17

问题

我尝试访问JDK17中的包com.sun.tools.javac.code。有人说可以添加以下VM参数:

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

pom.xml

<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <java.version>17</java.version>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</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>${java.version}</source>
                <target>${java.version}</target>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <fork>true</fork>
                <compilerArgs>
                    <arg>-Werror</arg>
                    <arg>-Xlint:all</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

然而,我仍然遇到编译错误:
error: package com.sun.tools.javac.code is not visible

我如何在jdk17中访问com.sun.tools.javac.code?我必须访问这个API,不想使用其他方式。

英文:

I tried to access the package com.sun.tools.javac.code in JDK17. Someone said can add the VM args as below:

                &lt;compilerArgs&gt;
                    &lt;arg&gt;-Werror&lt;/arg&gt;
                    &lt;arg&gt;-Xlint:all&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED&lt;/arg&gt;
                &lt;/compilerArgs&gt;

pom.xml

&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

&lt;groupId&gt;org.example&lt;/groupId&gt;
&lt;artifactId&gt;demo&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

&lt;properties&gt;
    &lt;java.version&gt;17&lt;/java.version&gt;
    &lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
&lt;/properties&gt;

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.8.0&lt;/version&gt;
            &lt;configuration&gt;
                &lt;source&gt;${java.version}&lt;/source&gt;
                &lt;target&gt;${java.version}&lt;/target&gt;
                &lt;showDeprecation&gt;true&lt;/showDeprecation&gt;
                &lt;showWarnings&gt;true&lt;/showWarnings&gt;
                &lt;fork&gt;true&lt;/fork&gt;
                &lt;compilerArgs&gt;
                    &lt;arg&gt;-Werror&lt;/arg&gt;
                    &lt;arg&gt;-Xlint:all&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED&lt;/arg&gt;
                    &lt;arg&gt;-J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED&lt;/arg&gt;
                &lt;/compilerArgs&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

</project>

However, I still got a compiler error:
error: package com.sun.tools.javac.code is not visible

How can I access the com.sun.tools.javac.code in jdk17?
I must access this API and don't want use other ways.

答案1

得分: 1

You'll need to add the --add-exports switch.
Specifically, "--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED".

从JDK 8迁移到较新的JDK版本.

If that does not work, try referring to the following question.
StackOverflow – error: package com.sun.tools.javac.util does not exist.

英文:

You'll need to add the --add-exports switch.
Specifically, "--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED".

Migrating From JDK 8 to Later JDK Releases.

If that does not work, try referring to the following question.
StackOverflow &ndash; error: package com.sun.tools.javac.util does not exist.

huangapple
  • 本文由 发表于 2023年6月25日 19:46:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定