【问题标题】:How to use jdk.compiler module in jshell如何在 jshell 中使用 jdk.compiler 模块
【发布时间】:2021-11-22 22:35:24
【问题描述】:

我的jdk版本是17,我想使用com.sun.tools.javac.file.JavacFileManager类,我的启动参数是

$ jshell --add-modules jdk.compiler

然后我输入了

import com.sun.tools.javac.file.JavacFileManager;

我收到这样的错误:

|  Error:
|  package com.sun.tools.javac.file is not visible
|    (package com.sun.tools.javac.file is declared in module jdk.compiler, which does not export it to the unnamed module)
|  import com.sun.tools.javac.file.JavacFileManager;
|         ^----------------------^

我该怎么办。

【问题讨论】:

    标签: javac jshell


    【解决方案1】:

    由于JEP 403,不允许访问 JDK 内部模块。 要在 Java 17 中明确允许访问,可以在启动 JVM 时使用选项--add-exports。这个选项似乎也适用于jshel

    jshell --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
    

    --add-exports 记录在案here:

    --add-exports module/package=target-module(,target-module)*
        Updates module to export package to target-module, regardless of module declaration. The target-module can be all unnamed to export to all unnamed modules. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多