0

I want my library/ jar in java to keep only its behavior (exposed interfaces) public and want rest of the stuff non-public for outside world. To keep the code modularized I also want to use different packages.

How can I made classes accessible to each other from different packages within the same jar, but not accessible from any client outside of the jar?

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
Manojkumar Khotele
  • 963
  • 11
  • 25

3 Answers3

1

You can't easily do this in ordinary Java, though OSGi makes it possible. The better way is usually to create a separate API library with interfaces and have your "private" classes implement those interfaces. Then client code can use the interfaces to interact with your library but doesn't need to be aware of the particulars.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

You can use OSGi for that.

This is a good place to start knowing more about OSGi: http://www.vogella.com/tutorials/OSGi/article.html

0

You can obfuscate all your code save from the public interfaces you wish to maintain, i.e. with proguard.

Obfuscating your code will not make it unaccessible per se, but it will make it very hard to utilize.

Mena
  • 47,782
  • 11
  • 87
  • 106