Java: expose only one package in jar file

I would like to have a jar file in which only the API package is available. All other packages (containing implementations) will not be available to another bank (or another class).

Is it possible?

If so, how?

+7
java jar
Dec 08 2018-10-12
source share
8 answers

Currently scheduled for Java 8 (2012?) JSR 294 . This JSR introduces more advanced modulation language constructs in Java.

Today, an implementation can be broken down into several packages. The subjects of such an implementation should be more closely related to each other than to the software environment. Today, designers are forced to declare program elements that are needed by other implementation subpages as public, thereby making them available around the world, which is clearly not optimal.

Alternatively, the entire implementation may be placed in one package. This fixes the problem above, but is cumbersome and exposes all the internal parts of all sub-parts to each other.

The foreseen language changes will solve these problems. In particular, we expect to introduce a new concept of modules ( superpackages ) at the language level, where the existing general access control will be applied only within the language level module and access to the API from outside the module will be limited to the API that explicitly exports the module.

Now I believe that the expert group is still discussing the details of this concept. But I believe that one thing you could do is (source: Superpackages in Java 7.0 ):

superpackage com.myorg.myApp.model { member package com.myorg.myApp.model.swing; member package com.myorg.myApp.model.html; export com.myorg.myApp.model.swing.SEmployee; export com.myorg.myApp.model.swing.SDepartment; export package com.myorg.myApp.model.html; } 

In other words, for a given "superpack", you can determine what is and is not exported, regardless of visibility keywords such as public .

+3
Dec 08 '10 at 15:06
source share

If you have classes that should not be used, put them in a subpackage called "internal". Although this does not prevent others from using classes, this is convincing evidence that they should not.

Example:

 my.library.Stuff my.library.StuffFactory my.library.internal.StuffImpl my.library.internal.MoreStuff my.library.internal.xml.DataStuff 

Although this is not a real solution, I would consider it a best practice.

+4
Dec 08 '10 at 15:40
source share

this is not realistic in Java 6 unless you are using OSGI, which I assume you are not using. what I usually do to hide classes is to use classes that are compatible with packages for implementations - but you still have to expose some classes if you are implementation classes that live in a separate package.

:

package com.example;

public class Service { } Suppose an API class uses ServiceImpl as an implementation

now, if ServiceImpl lives in one package, you can remove its public class modifier and it will not be available outside the package ...

if it lives in another package (your case), it should be publicly available:

package com.example.impl;

public class ServiceImpl { }

but all implementation details (related classes that it uses) from the same package should not be publicly available!

+2
Dec 08 2018-10-12
source share

In a โ€œnormalโ€ Java application this is not possible. Although in the OSGi environment you determine which packages are exposed by your package (slightly modified jar file) for other packages and are private (not visible outside your package).

+1
Dec 08 2018-10-12
source share

Why not just create a jar with API classes and a separate jar with implementations depending on one API. This way you can distribute your API classes without propagating your implementation.

KP.

0
Dec 08 2018-10-14
source share

I see the reason why you want to do this. I was always worried when I try to use someone's library. There are so many classes that I donโ€™t know where to start.

As far as I can see from most libraries, there is no library that tries to hide unrelated classes. Although this is not conclusive evidence, it means that there is no way to do this. Usually they run api and implementation into separate jar files.

However, if you run the application through the OSGi infrastructure, you can link your jar file so that only the API classes that you want can be seen in the OSGi frame. But using OSGi may work a little.

0
Dec 08 2018-10-12
source share

You can add an ant -task or maven-jar-plugin configuration to assemble the components you need, according to the preferences of your building tool.

0
Dec 08 2018-10-12
source share

Another solution is to set the default access modifier in the implementation classes and put them in the same package as the interfaces. Although he is a little dirty.

0
Dec 09 '10 at 11:00
source share



All Articles