如何在jshell中使用jdk.compiler模块

问题描述 投票:0回答:1

我的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个回答
1
投票

JEP 403 起,不允许访问 JDK 内部模块。 要在 Java 17 中显式允许访问,可以在启动 JVM 时使用选项 --add-exports

。此选项似乎也适用于 
jshell
:

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

--add-exports

已记录在
这里

--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.
    
© www.soinside.com 2019 - 2024. All rights reserved.