How to create my own java library(API)?

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 ;)


Solution 1:

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.

Solution 2:

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.

Solution 3:

You need to package your application as a jar file. You can use ant jar task to create jar files or you can use the jar command.

For ant tasks look at this link.

For creating it manually look at this link.