Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create my own java library(API)?

Tags:

java

jar

I created a program in Java and I designed it so that methods that I want them to appear (getter methods) in the main, I can call them easily after initiate the class that holds these methods.

The question is that, I need to make this application (that holds the getter methods) to be like an API so that I can give my application for developers to use my functions (the getter methods) if they need them, and only what they need is to add this file (I think the API after is done shown as .jar file).

How can I make it so that I can make my code reusable with other application? It's similar to the .dll, I think.

Thanks a lot ;)

like image 794
Q8Y Avatar asked Aug 31 '10 19:08

Q8Y


People also ask

What is Java API library?

The API (Application Programming Interface) is what a library looks like from the outside for a program that's using it. It's the "face" of a library to other programs. The API of a library is the set of publicly accessible classes, interfaces and methods in the library.


2 Answers

Create a JAR. Then include the JAR. Any classes in that JAR will be available. Just make sure you protect your code if you are giving out an API. Don't expose any methods / properties to the end user that shouldn't be used.

Edit: In response to your comment, make sure you don't include the source when you package the JAR. Only include the class files. That's the best you can really do.

like image 61
Zack Marrapese Avatar answered Sep 20 '22 07:09

Zack Marrapese


To be useable as an API, your classes should:

  • Use a unique package (ideally following the convention, i.e. the reverse of a domain you own as prefix). This prevents naming conflicts
  • Have only those classes and methods public or protected that are intended to be used by others. This makes it easier to use.
  • Have extensive Javadoc comments.
  • Be available as a JAR file - ideally separate JARs for binary distribution, source code and javadoc files.
like image 38
Michael Borgwardt Avatar answered Sep 17 '22 07:09

Michael Borgwardt