skies.dev

3rd Party Libraries Every Java Programmer Should Know

3 min read

Java core developer and author of Effective Java Joshua Bloch writes to

know and use the libraries.

Here are some of my favorite 3rd party Java libraries. ๐Ÿ‘‡

Lombok

I've heard people complain that Java is too verbose of a language.

That should not be the case when using Lombok.

This library lets you decorate your Java code with annotations to significantly reduce the amount of boilerplate code.

Check out this simple Person class:

import lombok.Value;

@Value
class Person {

  String firstName;
  String lastName;
}

The @Value annotation generates getters, setters, equals, hashCode, and toString.

Not a lot of boilerplate now!

Guava

If you ever find yourself writing a utility function to perform a simple task, check if there's a library method first.

Guava offers a vast array of useful utilities.

For example, they have many utilities for working with data structures:

import com.google.common.collect.Lists;

public class PrintArgs {

  public static void main(String[] args) {
    System.out.println(Lists.newArrayList(args));
  }
}

This barely scratches the surface though. There's a ton you can do with Guava.

Apache Commons

Apache Commons is another library with a great set of utility methods.

Here's a simple program that capitalizes all the arguments passed in to the program using Apache Commons.

import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;

public class CapitalizeArgs {

  public static void main(String[] args) {
    Stream
        .of(args)
        .map(StringUtils::capitalize)
        .forEach(System.out::println);
  }
}

JUnit

JUnit is the de facto unit testing solution for Java.

There are a lot of great integrations with Java IDEs whether you're using IntelliJ or Eclipse.

Here's a silly example showing some basic usage of the JUnit APIs to create tests.

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

public class BasicTest {

  @Test
  void checkBasicArithmetic() {
    assertEquals(2, 1 + 1);
    assertFalse("tan".equals("my favorite color"));
    assertNotNull(new Person("Sean", "Keever"));
  }
}

Jackson and/or Gson

Jackson and Gson are two libraries for working with JSON.

I'm grouping them together because they should largely be able to accomplish the same goal.

Here's a small example showing how you can stringify Java objects into JSON using Gson.

import com.google.gson.Gson;

public class PrintPersonJson {

  public static void main(String[] args) {
    if (args.length < 2) {
      throw new IllegalArgumentException("must give a first name and a last name");
    }
    String firstName = args[0];
    String lastName = args[1];
    Person person = new Person(firstName, lastName);
    System.out.println(new Gson().toJson(person));
  }
}

Apache Log4J

If you're working on a large project, you probably won't want to be logging solely with System.out.println.

Apache Log4J is a great Java logging solution.

Here's a basic example showing its usage.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class PrintArgLength {

  private static final Logger log = LogManager.getLogger(PrintArgLength.class);

  public static void main(String[] args) {
    if (args.length == 0) {
      log.error("please supply 1 argument");
      System.exit(1);
    }

    log.info("length of first arg: {}", args[0].length());
  }
}

Hey, you! ๐Ÿซต

Did you know I created a YouTube channel? I'll be putting out a lot of new content on web development and software engineering so make sure to subscribe.

(clap if you liked the article)

You might also like