Make classes public, but remain private to a JAR file

  • A+

Is there a way to make a Java class public but only public with a JAR file? AKA, it's not package private, instead it's package public, but it's not JAR public?

Say I have a structure like this:

project/   package1/   package2/   package3/ 

Since the 3 .java files are in different packages, I need to make things public. But is there a way to make them only public to the JAR that will contain the project? Such that if another project imports the JAR, it can't see certain public classes/field?


Using project Jigsaw in Java 9 you can do exactly that where you can decide on the packages you want to export. For example:

module com.yourproject {   exports com.yourproject.package1; } 

This will make sure that only package1 is exported.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: