Useful Open Source libraries for Java Programmers

Here is my collection of some of the useful third-party libraries Java developers can use in their application to do a lot of useful tasks. In order to use these libraries, Java developer should also be familiar with that and this is the whole point of this article. If you have an idea then you can research about that library and use it.

1. Logging libraries
Logging libraries are very common because you need them in every project. They are the most important thing for server-side application because logs are only placed where you can see what’s going on your application. Even though JDK ships with its own logging library, there are many better alternatives are available e.g. Log4j, SLF4j, and LogBack.

A Java developer should be familiar with pros and cons of logging library and know why using SLF4j is better than plain Log4j. 

2. JSON parsing libraries
In today’s world of web services and internet of things (IoT), JSON has become the go-to protocol to carry information from client to server. They have replaced the XML as the most preferred way to transfer information in a platform-independent way. Unfortunately JDK doesn’t have a JSON library yet but fortunately, there are many good third-party libraries which allows you to both parse and create JSON messages e.g. Jackson and Gson.

A Java web developer should be familiar with at least one of these libraries. If you want to know more about Jackson and JSON, I

3. Unit testing libraries
Unit testing is the single most important thing which separates an average developer from a good developer. Programmers often are given excuses for not writing unit tests but the most common excuse for avoiding unit testing is lack of experience and knowledge of popular unit testing library e.g. JUnit, Mockito, and PowerMock.

I have a goal in 2019 to improve my knowledge of unit testing and integration testing libraries e.g. JUnit 5, Cucumber, Robot framework etc.



4. General purpose libraries
There is a couple of very good general purpose, third-party library available to Java developer e.g. Apache Commons and Google Guava. I always include these libraries in my projects because they simplify a lot of tasks. As Joshua Bloch has rightly said in Effective Java (now the 3rd edition is also available) that there is no point in re-inventing the wheels and we should prefer using tried and tested libraries instead of writing our own routines every now and then.

It’s just for a good Java developer to get himself familiar with Google’s Guava and Apache commons library.

5. Http libraries
One thing I don’t like much about JDK is their lack of support for HTTP. Though you can make HTTP connection using classes in java.net package it’s not as easy and seamless as by using open source, third-party libraries like Apache HttpClient and HttpCore.

Though JDK 9 is bringing the support of HTTP 2.0 and better support for HTTP, I strongly suggest all Java developers get familiar with popular HTTP client libraries e.g. HttpClient and HttpCore.

 

6. XML parsing libraries
There are many XML parsing libraries exists e.g. Xerces, JAXB, JAXP, Dom4j, Xstream etc. Xerces2 is the next generation of high performance, fully compliant XML parsers in the Apache Xerces family. This new version of Xerces introduces the Xerces Native Interface (XNI), a complete framework for building parser components and configurations that is extremely modular and easy to program.

The Apache Xerces2 parser is the reference implementation of XNI but other parser components, configurations, and parsers can be written using the Xerces Native Interface. Dom4j is another flexible XML framework for Java application. 

7. Excel reading libraries
Believe it or not but all real-world application has to interact with Microsoft office in some form or other. Many application needs to provide functionality to export data in Excel and if you have to do same from your Java application then you need Apache POI API.

This is a very rich library which allows you to both read and write XLS files from Java program. You can see that link for a working example of reading Excel file in Core Java application.

8. Bytecode libraries
If you are writing framework or libraries which generate code or interact with bytecodes then you need a bytecode library. They allow you to read and modify bytecode generated by an application. Some of the popular bytecode libraries in Java world are javassist and Cglib Nodep.

The Javassist (JAVA programming ASSISTant) makes Java bytecode manipulation very simple. It is a class library for editing bytecodes in Java. ASM is another useful bytecode editing library.

 

Related blog:

 

Java course training in chennai

 

Benefits of Maven for java developers

Here is my collection of some of the useful Maven tips for Java and JEE developers. Since Maven is not only used to build core Java projects but also web and enterprise applications, these tips will help both core Java and web developers alike.

1) Build Tool
A Maven is a build tool like ANT which is used to create deliverable like JAR, WAR and EAR file in Java environment. Though there are a lot of differences between ANT and Maven like later is very easy to use because of the minimal configuration required.

As Maven uses convention over configuration which means lots of sensible default values.

2) Dependency Management
Main Benefit of using Maven for building Java application is dependency management. Since Java application depends upon many internal and external open source libraries with different versions, it makes sense to have an internal maven repository to archive all versioned JAR file of both internal and external libraries and let every application developer import them automatically using Maven while building an application. Maven itself maintains libraries as known as global maven repository with URL.

3) Transitive Dependency
Maven also manages transitive dependencies for you, what this means is when you include Spring framework as a dependency in your project, you don’t need to download all the libraries on which Spring is dependent.

Maven will download those JARs for you and it will also take care of version, this is a seriously good feature to save a lot of time which goes to setup build environment and then deal with nasty errors like UnSupportedClassVersionError and NoClassDefFoundError which generates due to incorrect versions of dependent libraries.

4) Configuring Maven
In order to use Maven, you need to install Apache Maven in your machine configure settings.xml, which contains the name of local repository and connection detail to connect to the global maven repository. Please follow the steps given in this guide to install Maven in Windows 8 machine.

5) M2Eclipse Plugin
If you are using Eclipse IDE for Java development then you can also use M2Eclipse Maven plug-in which gives you the ability to create, modify and run maven projects from Eclipse. M2E Eclipse plug-in can be downloaded and installed from Eclipse Marketplace.

When you use Maven from Eclipse then you can also build your Maven project by using M2Elcipse plugin, just right click and choose Maven Install or Maven Build option as shown in below image:

6) Pom.xml
Another important thing in Maven is pom.xml, this is similar to build.xml of ANT build tool and for every project, you have their pom.xml which stand for Project object model. This pom.xml contains project details e.g. artifactId, groupId, name, etc which is used to create deliverable like JAR file and can be used to upload JAR in the internal maven repository. 

The pom.xml also contains Maven plugins used in a project like JAR plug-in which is responsible for creating a JAR file from class files and resources. pom.xml also contains all dependency e.g. JAR files and their version required by your Java project.

7) Task vs Goals
You can build and run Maven project either from the command line or Eclipse itself. Similar to task Maven has goals and you can run those goals by invoking them using the mvn command in the command line.

In order to build and run a Maven project from the command line, you need to include maven binaries like MAVEN_HOME/bin in your PATH. If you have M2Eclipse plugin installed then you can directly perform maven clean, maven installs from Eclipse itself. clean and install are two frequently used maven goals.

8) Convention over Configuration
Once Maven is installed and configured on the local machine, you can create Maven projects. Maven project enforces a standard directory structure for organizing source, resource, and test classes. They follow convention over configuration which means all Java classes will go to the src/main/java folder and all test classes will go to the src/main/test folder.

You can further check Apache Maven: Beginner to Guru book to learn more about Maven convention over configuration concept and how it helps you to create and build your project and understand the structure of other open source Java projects which use Maven.

9) Target Directory
When you build the project using Maven it creates class files and copies resource in the target directory. You can find all your class files inside the target/classes directory. When you run the maven clean command it cleans up this directory to compile your Java files again and create new class files.

On the other hand, mvn install command runs both maven clean and maven build together. The maven clean will clean old build artifacts e.g. class files from the target directory.

That’s all about some of the useful Maven tips for Java developers. Even though now Gradle, another build tool written in groovy, is capturing some ground of Maven, it is still the most popular build tool for Java projects. Gradle will take its time because it has a little bit of learning curve in terms of the Groovy and domain-specific language you use to write the build script but given Maven uses XML, anybody can understand the build process and build dependencies.

 

Related blog:

Java training institute in chennai

Multiple inheritance in Java explained

Multiple inheritance is a feature of some object-oriented computer programming languages. This is where an object or class can come into characteristics and features from one or more parent object or parent class.

Java does not support multiple inheritance because it can cause ambiguity in various scenarios, mainly because of the ‘diamond problem’. Java is designed to be a simple programming language.

The problem is that is a class extended two other classes, and both superclasses had, for example, a doStuff() method, which version of doStuff() would the subclass inherit? This type of scenario is typically known as ‘Diamond Problem’ also known as ‘Deadly Diamond of Death’, and because of the shape of the class diagram that can be created in a multiple inheritance design.

The diamond is formed when classes B and C both extend A, and both C and C have overridden the method in A. Class D has, in principle, inherited two different implementations of the identical method.

When drawn as a class diagram, the shape of the four classes looks the same as a diamond.

After the introduction of Default Methods in Java 8, the interfaces can also have the method bodies. In Java 8 interface, it can have method definition using Default Methods, then clearly it should also result in ambiguity? Yes, but Java 8 can handle this type of compatible problem.

In Java 8, you cannot implement multiple interfaces having the same signature, without explicitly overriding the methods in the Child class. It is possible to call the Parent interface method explicitly from the Child class.

Explaining the inheritance exception for Java 8 may not be necessary but David believes this may show extra credibility knowing this extra bit of information.

David also discusses his love for snippets of code being projected onto a wall or are shown to you in a reasonably easy-to-read form, and you are asked to describe what they mean, what they print, and what value of a variable would be.

However, the types of questions he isn’t too fond of are coding exercises in which you need to type. Especially, if it is in an editor platform or IDE that is unfamiliar, as he believes this is not a test of knowledge.

David’s advice for candidates going through the Java interview process is to understand that when they are asking questions and evaluating you on your answers, it is entirely appropriate to evaluate them on the content of those questions and how they react to your answers. His indication of a good company is: ‘if this company was public, would I buy stock in them?’


And that is our round-up of the popular Java interview questions you may come across. These questions have been outlined on how to best answer during an interview, however, it is advised to do as much research as possible to ensure you answer the question as best as you can.

 

Related blog:

Java training in chennai