Friday, July 21, 2023

4 Best practices to name your JAR file in Java

If you are an author of an internal, proprietary Java library or an external open-source library,  or you are one of those lucky developers who ship Java applications by yourself then you should follow these best practices while naming your JAR files. These best practices are a result of the practical experience of using hundreds of Java libraries and applications. Following these best practices will help in better management of JAR files. It's part of my other best practices articles like best practices while naming a variable, writing comments, overriding methods, multi-threading, JDBC, and best practices while dealing with passwords. If you are interested in learning more about best practices, you should read those articles.

You can also take a look at Effective Java, the mother of all best practices in the Java world, and one of the must-read books for any Java developer. Joshua Bloch, author of this book has also authored several key classes in JDK like Java Collection Framework and other important classes in java.lang, and java.util packages.




4 Best Practices for Naming JAR Files in Java 

Now let's see some best practices while naming your JAR file:

1) Give them a specific name, prefer concrete over abstract
For example, util.jar is the worst jar file name I can think of. All I know is it contains some utility/helper classes. But it is unclear if they belong to an application server, or a framework, or any application sub-systems. You will need to ask someone or refer to docs to know what it is. It's part of the DataDirect JDBC Driver distribution. On the other hand, good jar file names are always self-explanatory, for example, struts.jar or spring-mvc.jar




2. Use hyphen (-) instead of underscore (_) as a word separator
There are two reasons I advise you to prefer hyphen(-) over underscore(_). First, it's easier to type hyphen than underscore; secondly, when the file name is underlined (like in a hyperlink), the _ is invisible. Hyphen has been shunned in file names partly because it is an illegal character in java identifier. But this concern is unwarranted. One of the examples of this best practice is spring-mvc.jar which uses a hyphen to join two words.


3. Append version number if distributed standalone
If your JAR file is distributed standalone then you must append the version number on the name of the JAR file because they can be dropped into any applications, which may need specific versions. We don't want users to have to compare file size or extract some META-INF files to know its version. Some examples of this best practice are hibernate3.jar, commons-logging-1.0.3.jar, and log4j-1.2.8.jar.


4. Always use extension ".jar", not ".zip"
Theoretically, jar files can have any extension or no extension at all. If you specify it in the system classpath, it should be loadable. The problem is with automatic library detection and loading by containers and frameworks. It's expensive to scan all files so some sort of extension restriction is needed. For this reason, J2EE/JavaEE platform spec requires all library jar files to use ".jar" extension, such as WEB-INF/lib/mybeans.jar.

Here is a summary of the above 4 best practices Java developers should follow while naming their JAR files:



That's all on best practices to name your JAR file in Java. Let me know what are your thoughts about these practices, do you already follow them? Do they make sense? If you are following any other best practices then let us know as well.


Further Reading
  1. Best Practices to name your variable, class, and methods (see here)
  2. Best Practices to follow while overriding methods in Java (see here)
  3. Top 10 Multithreading and Concurrency Best Practices in Java (see here)
  4. Top 10 JDBC Best Practices for Java Programmer (see here)
  5. Best Practices while dealing with passwords in Java (see here)
  6. 10 Best Practices to follow while writing code comments (see here)
  7. 10 Exception handling Best Practices in Java (see here)
  8. Best Practices while writing JUnit tests in Java (see here)
  9. Best Practices to Avoid NullPointerException in Java (see here)
  10. Effective Java - Mother of All Best Practices in Java (see here)

Thanks for reading this article so far. If you find these Java best practices for naming JAR files then please share them with your friends and colleagues. If you have questions please ask. 

1 comment:

  1. 1, 3, and 4, all good ones with huge, vast benefits arising from following those practices. Number 2 is kinda weak. It becomes invisible in a URL when it's underlined in a web page? Really? That's the best reason in favor? That's really quite minor.

    ReplyDelete