Say No To Bulky Codes

A captivating walk through Java Libraries and Frameworks

Sandumini Nayanathara
Javarevisited
5 min readAug 16, 2020

--

Photo by Caleb Russell on Unsplash

To become a good Java developer, just knowing Java syntax is not enough. There is so much more to work out to become a better Java developer. One such is to have a sound knowledge of Java libraries and frameworks, and learning how and when to use them.

Java libraries are packages that contain classes or interfaces that fall into a certain category. These classes and interfaces contain methods that you can use in your program to address your programming-related issues.

While Frameworks can be anything we use in developing applications- a library, a collection of libraries, or a collection of scripts, etc.

There are tons of libraries and frameworks available, so being familiar with at least a few of the most used is very important. So I will share with you some library categories which I thought Java developers, no matter whether you are an expert or a newbie should be aware of.

1. Logging Libraries

Using print statements to print messages to the console for testing and debugging a Java program is a common practice. But this method of debugging is not that effective, as scanning hundreds of lines could be exhausting. Also, by experience, you all know that these lines of debugging you add, make your code cluttered and a mess.

Logging libraries come in handy at such instances. These libraries are vital, especially in server-side applications, as you cannot see what’s going on inside servers, log files must be used for this purpose.

The java.util.logging package comes along with the JDK, but there are much better logging frameworks available like Flogger, SLF4j, LogBack, and also Log4j2(Log4j).

You can learn more about these here.

2. Unit Testing Libraries

A Java developer should ensure the quality of the software he develops and should catch bugs as soon as possible. For those, Unit testing is a good approach.

Unit testing means breaking your code into the smallest pieces of testable codes and subjecting each piece to a series of tests to verify if the purpose of that code is satisfied or not.

Simply, it means — writing a piece of code(unit test) to verify the code (unit) written for implementing requirements. But most developers skip this step as they think this is extra work or due to a tight schedule.

But writing good unit tests is important because it is better to detect and fix bugs during this test and save both money and time rather than finding bugs at the latter stages of software development.

Writing own codes for unit testing is a lot of work and it is not necessary. This Stack Overflow answer will show you an example of writing unit tests manually.

A library like JUnit, AssertJ, TestNG, Mockito, Wiremock, and PowerMock can be used, for you to get the job done.

So be familiar with using unit testing libraries. It will help you to detect bugs early, improve code quality, save money and time, and many more benefits.

For unit testing debug,info, or warn log messages the above mentioned frameworks wouldn’t work. The LogCaptor library would help you out at these instances.

3. Database Connection Pooling Library

Connection pooling is a data access pattern used by software applications to connect to databases.

This is done by a set of existing, reusable connection objects. Once a new connection is required, a connection object in the pool is retrieved, and then it is placed back in the pool after the thread which used is completed.

If not for this data pattern, database connections are quite expensive. We can use libraries like Commons Pool and DBCP, as creating database connections at runtime takes time and makes request processing slower.

4. HTTP Libraries

There is a built-in Java class HttpUrlConnection which allows us to perform basic HTTP requests without the support of any third-party libraries. All the classes needed for this are contained in the java.net package.

But the code written with the help of HttpUrlConnection can be bulky and it does not provide advanced functionalities, full flexibility, and the user-friendliness needed.

Therefore, we must use third-party libraries to fill this void. Some of those libraries which we can use are Apache HttpComponents Client, Jetty, and Spring 5 WebClient.

But note that starting from JDK 11, Java provides a new API, the HttpClient API, as a replacement for the HttpUrlConnection. This supports modern web features without the need to add third-party libraries.

5. Messaging Libraries

A Message, as you know is a piece of information — a text, XML document, or JSON data, etc. Then messaging means the exchange of information between software applications or software components.

JMS(Java Message Service), which is provided by Java, is used to create, send, receive, and read messages between different systems. You can also use Tibco RV messaging protocols to get the job done.

6. JSON Parsing Libraries

The use of web-based applications is rapidly growing. This has resulted in increasing the use of JSON.

JSON is used as a communication protocol between server and client. Earlier this task was done using XML and now it is almost completely replaced by JSON. This is because JSON requires less coding and has a smaller size, making it faster to process and transmit data.

In JSON parsing, the JSON data received is read, then interpreted as a list object, and presented it to the end-users in a user-friendly way.

Although, there is no in-built JSON library in JDK you can use third-party libraries like Jackson, Gson, JSON.simple, JsonParser, Genson. The best one for your project must be decided by you accordingly.

Here is some help for you in selecting the library which suits best to your project.

7. General Purpose Java Libraries

There are some really exciting third party libraries available which you can use to make coding a lot more simplified and cleaner. Here are some of them;

This is a project which is concerned with creating Java libraries- Commons Math, Commons CLI, Commons CSV, and Commons IO are some commonly used Apache Commons libraries.

Provide solutions for many problems encountered in projects like collections, Maths, strings, inputs, and outputs.

Lombok is a very useful library, especially this helps to avoid repetitive code. For example, you know that getters and setters of a program take a considerable number of lines of code, making it cluttered. But with Lombok, you can have a cleaner code as it generates the repetitive code but does not make it visible in the code.

As I mentioned at the beginning there are tons of libraries and frameworks available. Explaining them in detail to the depth is impossible and it will make this article too lengthy.

This article was only meant to enlighten you on how Java libraries and Frameworks can help you out from boilerplate code. So there are many more things that you should investigate by yourself about the above-mentioned library categories as well as about the ones I haven’t touched. Explore more about their pros and cons that will give you an idea about what best suits your program.

I hope this was helpful. Hope to see you with more articles soon. Thank you for reading.

--

--