By Alexander Mills

2019-02-11 09:40:58 8 Comments

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:


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?


@Sleiman Jneidi 2019-02-11 09:49:38

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.

@nullpointer 2019-02-11 10:56:47

Pointed already by existing answers, the way to adapt to this is modularising your code (adding a module declaration with for a start). Just detailing them out with reference to sotms#module-declaration

exports clauses can be added to declare that the module makes all, and only, the public types in specific packages available for use by other modules:

module {
    requires org.baz.qux;

If a module’s declaration contains no exports clauses then it will not export any types at all to any other modules.

and from the JLS section Exported and Opened packages

The exports directive specifies the name of a package to be exported by the current module. For code in other modules, this grants access at compile time and run time to the public and protected types in the package, and the public and protected members of those types (§6.6). It also grants reflective access to those types and members for code in other modules.

and further related to qualified and unqualified exports...

For a qualified directive, the public and protected types in the package, and their public and protected members, are accessible solely to code in the modules specified in the to clause. The modules specified in the to clause are referred to as friends of the current module. For an unqualified directive, these types and their members are accessible to code in any module.

@Alexander Mills 2019-02-11 11:18:23

so you just throw this file there somewhere? could you perhaps show how thr structure might look?

@nullpointer 2019-02-11 11:42:01

@AlexanderMills here is an official quick start guide for that. You keep that file at the project level in the hierarchy.

@Lutz Horn 2019-02-11 09:43:47

Yes, Java 9 introduced modules which are designed to solve this kind of problem.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Dealing with "Xerces hell" in Java/Maven?

24 Answered Questions

6 Answered Questions

[SOLVED] parent package class accessible from child packge class in java?

  • 2010-08-25 09:55:49
  • Vinoth Kumar
  • 10825 View
  • 6 Score
  • 6 Answer
  • Tags:   java

27 Answered Questions

[SOLVED] How to add local jar files to a Maven project?

35 Answered Questions

8 Answered Questions

[SOLVED] Java: Multiple class declarations in one file

  • 2010-02-25 18:45:29
  • Michael Brewer-Davis
  • 221257 View
  • 218 Score
  • 8 Answer
  • Tags:   java class

42 Answered Questions

32 Answered Questions

[SOLVED] Can't execute jar- file: "no main manifest attribute"

15 Answered Questions

[SOLVED] Efficiency of Java "Double Brace Initialization"?

33 Answered Questions

[SOLVED] Making a private method public to unit test it...good idea?

  • 2011-08-16 09:11:42
  • jcvandan
  • 33470 View
  • 274 Score
  • 33 Answer
  • Tags:   c# java unit-testing

Sponsored Content