Package is not visible error
Asked Answered
D

2

15

I was trying to use joptsimple package in my project but I get following error:

Error:(4, 20) java: package jdk.internal.joptsimple is not visible
  (package jdk.internal.joptsimple is declared in module jdk.internal.opt, which does not export it to the unnamed module)

Any ideas what should I do with it?

Darwin answered 5/1, 2018 at 19:56 Comment(3)
Does it mean there is no way i can import this package?Darwin
if this package is not exported in the module you are using, then you can't use this package.Towers
Do you have JOpt Simple on your class path? I wonder if this is just your IDE suggesting you import jdk.internal.joptsimple.OptionParser rather than joptsimple.OptionParser? If so, that would be an IDE bug as it should never automatically import a non-exported package.Parnell
S
8

Well, the message clearly reads (in fact notice the beautiful naming convention by JDK devs "internal" meaning they don't want to expose them) that the API you're trying to access is in a package that is not exported to any other module except these -

exports jdk.internal.joptsimple to jdk.jlink, jdk.jshell

even if you add requires jdk.internal.opt module in your declaration.


To make use of classes from these packages should be avoided.

Also since even the Open JDK itself integrates JOpt Simple for internal usage by JDK tools. I would suggest you make use of the library jopt-simple for a long-term solution by adding the dependency on your modulepath(java9) or classpath(java8) using -

Maven -

<dependency>
    <groupId>net.sf.jopt-simple</groupId>
    <artifactId>jopt-simple</artifactId>
    <version>6.0-alpha-2</version>
</dependency>

Gradle:-

compile 'net.sf.jopt-simple:jopt-simple:6.0-alpha-2'

After the inclusion of which the package name in use in your classes shall simply be joptsimple.* instead of jdk.internal.joptsimple.*.

Sociology answered 6/1, 2018 at 9:54 Comment(0)
H
8

You are apparently using an internal API, which you shouldn't. Such APIs are not standardized - they can differ from JVM to JVM and may change at any time without notice.

That said, if you still want to use it, you can do so by adding the following command line flag at compile and run time (i.e. to javac and java):

--add-exports jdk.internal.opt/jdk.internal.joptsimple=ALL-UNNAMED

If you are creating a module you must replace ALL-UNNAMED by your module name.

Hexamethylenetetramine answered 5/1, 2018 at 22:39 Comment(2)
If you are creating a module you MUST replace ALL-UNNAMED by your module name, or else it won't work.Lupien
Absolutely, @MichaelBöckling, I just fixed the phrasing. Thanks for pointing it out.Hexamethylenetetramine
S
8

Well, the message clearly reads (in fact notice the beautiful naming convention by JDK devs "internal" meaning they don't want to expose them) that the API you're trying to access is in a package that is not exported to any other module except these -

exports jdk.internal.joptsimple to jdk.jlink, jdk.jshell

even if you add requires jdk.internal.opt module in your declaration.


To make use of classes from these packages should be avoided.

Also since even the Open JDK itself integrates JOpt Simple for internal usage by JDK tools. I would suggest you make use of the library jopt-simple for a long-term solution by adding the dependency on your modulepath(java9) or classpath(java8) using -

Maven -

<dependency>
    <groupId>net.sf.jopt-simple</groupId>
    <artifactId>jopt-simple</artifactId>
    <version>6.0-alpha-2</version>
</dependency>

Gradle:-

compile 'net.sf.jopt-simple:jopt-simple:6.0-alpha-2'

After the inclusion of which the package name in use in your classes shall simply be joptsimple.* instead of jdk.internal.joptsimple.*.

Sociology answered 6/1, 2018 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.