Monday, February 22, 2021

Fix Cannot make a static Reference to The Non-static Method Error

When a person moves beyond the "Hello world" program where everything is with in a main method and writes a program with more than one method, one error he or she surely will encounter is "Cannot make a static reference to the non-static method or a non-static field".

Not only beginners even experienced programmers unknowingly keep making the mistake of trying to access a non-static method or field from a static context and get a reminder from compiler that they have forgotten the basics! At least I do that many times when I write a program to test something and from main method (which is static) I try to access some non-static method and get the error "Cannot make a static reference to the non-static method or a non-static field".

Sunday, February 21, 2021

Difference Between Encapsulation And Abstraction in Java

Encapsulation and Abstraction are two of the fundamental OOP concepts other two being Polymorphism and Inheritance. Some times people do get confused between encapsulation and abstraction as both are used to "hide something". So in this post let's try to see the difference between encapsulation and abstraction in Java.

First let's try to define these two OOPS concepts encapsulation and abstraction to get a better idea-
  • Encapsulation- Encapsulation means keeping together the implementation (code) and the data it manipulates (variables). Having proper encapsulation ensures that the code and data both are safe from misuse by outside entity. So, in a way Encapsulation is more about data hiding.

    It is a Java class which is the foundation of encapsulation in Java.

    Following image shows the concept of encapsulation. Fields and methods are encapsulated with in the class and the methods of the class can access and manipulate fields. Outside entity can't access fields directly but need to call methods which in turn can access data.

Saturday, February 20, 2021

How to Create PDF in Java Using OpenPDF

In the post Creating PDF in Java Using iText we have already seen how to use iText library to generate a PDF in Java. Itext is one of the best way to generate PDF in Java, has many features but there is one problem; it is AGPL licensed which means you must distribute all source code, including your own product and web-based applications. Many times the idea to use iText is shot down by the clients because of this reason. So, in this post we’ll see one alternative of using iText for generating PDF in Java. That option is OpenPDF for generating PDF.

OpenPDF for creating PDF in Java

OpenPDF is a free Java library for creating and editing PDF files with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. In fact you will find the code for generating PDF using OpenPDF quite similar to iText API till version 5. OpenPDF is actively maintained and a very good option for creating PDF in Java.

Maven dependecy

For using OpenPDF library you must add the following dependencies to your pom.xml file.

For Java 8 onward-

<dependency>
  <groupId>com.github.librepdf</groupId>
  <artifactId>openpdf</artifactId>
  <version>1.2.4</version>
</dependency>
Java 7 compatible branch-
<dependency>
  <groupId>com.github.librepdf</groupId>
  <artifactId>openpdf</artifactId>
  <version>1.2.3.java7</version>
</dependency>

Friday, February 19, 2021

Convert HTML to PDF in Java + Openhtmltopdf and PDFBox

In this tutorial we’ll see how to convert HTML to PDF in Java using Openhtmltopdf and PDFBox.

Check another option to convert HTMP to PDF in this post- HTML to PDF in Java + Flying Saucer and OpenPDF

How does it work

Let’s first understand what do the libraries mentioned here do-

  1. Open HTML to PDF is a pure-Java library for rendering arbitrary well-formed XML/XHTML (and even HTML5) using CSS 2.1 for layout and formatting, outputting to PDF or images.
  2. jsoup library is used for parsing HTML using the best of HTML5 DOM methods and CSS selectors. That gives you a well formed HTML (XHTML) that can be passed to the Openhtmltopdf.
  3. Openhtmltopdf uses the open-source PDFBOX as PDF library which generates PDF document from the rendered representation of the XHTML returned by Openhtmltopdf.

Thursday, February 18, 2021

Creating PDF in Java Using Apache PDFBox

In the post Creating PDF in Java Using iText we have already seen how to use iText library to generate a PDF in Java, we have already seen one alternative of iText which is OpenPDF for generating PDF. In this tutorial we’ll learn about another option for generating PDF in Java using Apache PDFBox.

PDFBox for creating PDF in Java

The Apache PDFBox library (https://pdfbox.apache.org/) is an open source tool written in Java for working with PDF documents. Using PDFBox you can create new PDF documents, manipulate existing documents and extract content from PDF documents. Apache PDFBox also includes several command-line utilities. Apache PDFBox is published under the Apache License v2.0.

Wednesday, February 17, 2021

Java Program to Find First Non-Repeated Character in a Given String

A very popular interview question for String is to write a Java program to find first non-repeated character in a given String. For example if given string is “always” then first non-repeated character is ‘l’ as character ‘a’ is repeated. Same way if String is “net” then first non-repeated character is ‘n’.

There are many ways to solve this problem, in this post I am giving 4 solutions to find first non-repeated character in a given String using Java.

  1. If you are asked not to use any inbuilt API or data structure.
  2. Solution using LinkedHashMap.
  3. Solution using indexof() method of the String class.
  4. Java 8 onward you can also use Stream API

Wednesday, February 10, 2021

HTML to PDF in Java + Flying Saucer and OpenPDF

In your application you may come across a scenario to convert the HTML to PDF on the fly. In this tutorial we’ll see how to convert HTML to PDF in Java using Flying Saucer and OpenPDF.

Check another option to convert HTMP to PDF in this post- Convert HTML to PDF in Java + Openhtmltopdf and PDFBox

How does it work

Let’s first understand which library is used for what purpose-

  1. Flying Saucer is an XML/CSS renderer, which means it takes XML files as input, applies formatting and styling using CSS, and generates a rendered representation of that XML as output. As an input you can pass an XHTML file which is an XML document format that standardizes HTML.
  2. jsoup library is used for parsing HTML using the best of HTML5 DOM methods and CSS selectors. That gives you a well formed HTML that can be passed to the Flying Saucer.
  3. Flying Saucer renders the input XHTML that still needs to be converted to PDF for that OpenPDF is used. OpenPDF is a free Java library for creating and editing PDF files with a LGPL and MPL open source license. OpenPDF is based on a fork of iText.