简体   繁体   中英

Java - modular library project - how to?

I am supposed to deliver a SDK in Java for the company I work for. I have a few years of Java EE experience but not so much when it comes to develop API and SDK.

The problem here, is that the SDK is already available in .NET C# and working just fine. I made it myself. Switching to Java is a nightmare. I tried several solutions. All failed, some were inconclusive due to the fact I could not progress.

As the title says, I a need to develop a Java Library that has modules. Internal modules. Modules the world out there is NOT supposed to see / use / modify.

In C#, it's easy as pie : create your modules respective namespace, make their classes and methods privates, expose one or more wrappers (bridges) to the entire assembly (project) with internal so that the "main module", through it's own public wrapper accessible by the world, can use the tools provided within these internals modules.

The keyword, here, is INTERNAL. I think it's pretty easy to understand. So let's take an example. Let's say the SDK is consisting of 4 modules.

  1. The 1st module, is the main module, the one that is public and exposed to the whole world. In other words, the unique entry point of the SDK. It's like a master of its own universe. It can use the internal modules at will, but will never show them to the world. Never.
  2. The 2nd module is network-related. As in, it deals with network to manage connexions to remote services, read and write data from and to a stream. It offers its own little wrapper so that the main module does not need to use the 2nd module internal tools. Like a universe inside a universe.
  3. The 3rd module is a data processing module. It receives packets (hand-made by the 2nd module) so that it can be processed and relevant information dealt with.

So here we are. How can I do that in Java using NetBeans ?

I tried Maven with POM Project, and Netbeans modules. A nightmare.

I tried creating multiple libraries (one per module), tweaked the Main Module library to include its (modules) dependancies but it does not work. In a standard Java EE console application, as soon as I attempt to instanciate my SDK Manager (ergo the main wrapper from the main module), it fails because Class Not Found exception : could not find classes related to the internal sub-modules. If I add all modules respective Jar into this Java Console app, it can access all wrappers. Where is the fun in that ?

Thanks for the help !

Project Jigsaw will eventually give you what you want when java 9 comes out.

See this article about how jigsaw works, in particular,

An exports clause in a module declaration makes the public types in the package it names available to other modules, so we can with Jigsaw defines boundaries, and not all public types could be used from other modules, we must explicitly specify which types are visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM