How to create my own java library(API)?

JavaJar

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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.

Solution 4 - Java

Make sure you write and publish javadocs for all your public and protected classes and methods.

Solution 5 - Java

To create the jar:

jar cf <jar_name> <sources>

Solution 6 - Java

There are several ways you can expose your code. Creating a jar and distributing that may be the easiest as other developers will just have to include your jar. However, if you are talking about "anyone" accessing your code, a web service may make more sense as you can provide access to the data without providing all of the necessary code. You mention providing access to your getters - if you just create a class that has getters, the other developers can use them, but how are they going to be populated? If your application is self contained in that it gets the necessary data and provides the getters, that should work, but if you are talking about providing access to data from your running application, a web service makes more sense as your application can retrieve the data and provide access via publicly accessible methods.

You most likely want to create interfaces as well so developers can code against the interface and you can change the internal workings without impacting them. Any API that will be used by others should be extensively documented as well.

Solution 7 - Java

Well, depends on your IDE. I use Netbeans, so I just hit build project, and viola! A jar file is created in my directory specified. Now, that's just for compiling. All anyone has to do is download your .jar file, and if in Netbeans, right click libraries, add jar/folder, and select the downloaded file.

Solution 8 - Java

You can also consider:

  • Include some samples that demonstrate how to use your library
  • Build your jar using Apache Maven
  • Put your jar in a public maven repository
  • Publish a new version of your library as you find/fix bugs
  • If you want to hide your implementation, you can pack your jar with obfuscation, so that if someone decompiles your classes, the code will be difficult to read

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQ8YView Question on Stackoverflow
Solution 1 - JavaZack MarrapeseView Answer on Stackoverflow
Solution 2 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 3 - JavaCoolBeansView Answer on Stackoverflow
Solution 4 - JavaNoel MView Answer on Stackoverflow
Solution 5 - JavajmoreiraView Answer on Stackoverflow
Solution 6 - JavaTai SquaredView Answer on Stackoverflow
Solution 7 - JavagigyView Answer on Stackoverflow
Solution 8 - JavaelyorView Answer on Stackoverflow