Jan Hadáček
Jan Hadáček

Reputation: 187

Equivalent to C++ header files in Java?

I've got a large Java library and I want to develop several smaller applications that interface this library. The library will be present on the target device's class-path as JAR, but I would like to avoid the need to have the entire library (either JAR or source) present at compile-time if possible. (If it matters, the JAR is quite huge and I want to protect intellectual property too, though it's not my primary concern.)

In C++, I would solve this issue by creating a DLL (.so) and copying just the relevant class and function definition headers to my project and adding them to include path at compile time, let the dynamic linker do the job at runtime.

How to do this in Java? One idea I have would be to remove private methods/members, and strip methods of relevant classes so that their bodies are empty and the real classes and methods with same signatures are loaded at runtime. However, this approach seems quite ugly and rudimental, plus some tool would be needed to automate this process. Is there a tool to do this? Is there a better way?

I don't think it's a duplicate of this question. The point of that question is to minimize size of the resulting JAR file at compile time, by removing unnecessary classes. My point is not to remove unused definitions, but to avoid the need of having the complete library JAR at compile time at all. Though these are simmilar and there may be a way how to achieve what I want using ProGuard, the linked question does not discuss it.

Upvotes: 2

Views: 3203

Answers (2)

0bot
0bot

Reputation: 149

If your problem is only due to the minimization of the final jar and you use Apache Maven, you could try to use the option "provided" when you declare a dependency in the pom.xml.

For example I use this dependency declaration:

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.48</version>
        <scope>provided</scope>
    </dependency>

This means that my java compiler use the bouncycastle library to compile, but finally it doesn't include it in the final jar. You should provide it during execution.

Upvotes: 2

Jiri Tousek
Jiri Tousek

Reputation: 12440

There's no exact equivalent for header files in Java, but compiling against the "header" (in the meaning of "contract") without having the actual implementation can be achieved using interfaces:

  • Create interface for every class you want the "header" for, with relevant methods
  • Make the actual classes implement the respective interfaces
  • Pack the interfaces into one JAR, and the implementations into another one
    • (if you're using a build tool like Maven, use two projects, and let the implementation project depend on the interface one)
  • Only provide the interface JAR at compile time, and both at run time

There of course will need to be some artifact that knows the actual implementations and can instantiate them, or you'll have to use some lookup that searches classpath for a suitable implementation.

Upvotes: 4

Related Questions