Search code examples
javavisibilityjava-11java-module

How do I use a JDK class that isn't visible to me?


I am new to Java so please bear with me.

I am trying to use the JDK class "com.sun.tools.example.debug.expr.ExpressionParser". When I compile my code, I get the following error: "package com.sun.tools.example.debug.expr is declared in module jdk.jdi, which does not export it".

Ok fair enough, but is there anything stopping me from creating my own version of this module with custom visibility of its packages? I have all the source files that I need, which to me sounds like it should be possible to compile this module such that it exports package "com.sun.tools.example.debug.expr" and can be utilized by my Java program.

My situation may be an XY problem, but I see no other obvious workaround. I want to use a JDK class that seems right at my fingertips but I'm blocked by package visibility. I understand if this is discouraged, but I'd appreciate to learn if this is even possible even at least in theory.


Solution

  • IntelliJ returns the following.

    "Package 'com.sun.tools.example.debug.expr' is declared in module 'jdk.jdi', which does not export it to the unnamed module"

    It's solution is

    "Add '--add-exports jdk.jni/com.sun.tools.example.debug.expr=ALL-UNNAMED' to module compiler options"

    The --add-exports appears to be a java command option.

    Here is a reference, Migrating From JDK 8 to Later JDK Releases.