javacjshell

How to use jdk.compiler module in jshell


my jdk version is 17 and i want use com.sun.tools.javac.file.JavacFileManager class, My startup parameters are

$ jshell --add-modules jdk.compiler

then i typed

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

I got a error like this:

|  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;
|         ^----------------------^

What should i do.


Solution

  • Access to JDK internal modules is not allowed since JEP 403. To explicitly allow access in Java 17, one can use the option --add-exports while starting the JVM. This option also seems to work with jshell:

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

    --add-exports is docummented 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.