Usage of class sun.misc.Unsafe

This post is next update in sequence of discussions regarding little known features of java. Please subscribe through email to get updated when next discussion goes live. And do not forget to express your views in comments section. Java is a safe programming language and prevents programmer from doing a …

Guide to Java SerialVersionUID

Java SerialVersionUID example. Learn to create generate serialVersionUID for classes to be serialized. Learn to change serialversionuid correctly.

How does a Web Servers Work?

Today the line between “app server” and “web server” is fuzzy. But people continue to use the terms differently, primarily for emphasis.

Decorator Pattern

In software engineering, the decorator design pattern is used to add additional features or behaviors to a particular instance of a class without modifying the other instances of the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be …

Visitor Design Pattern Example

Design patterns are used to solve the problems which occur in a pattern, we all know that, right? Also we know that behavioral design patterns are design patterns that identify common communication patterns between objects. One of such behavioral patterns is visitor pattern, which we are going to learn about …

JavaScript: Correct way to define global variables

We know that what global variables or constants are, these are fields which are accessible application wide. In java, it is done usually done using defining “public static” fields. Here by adding final keyword, we can change the global variable to global constant. So easy, right? But what about javascript? …

Why are Strings Immutable in Java?

The immutable strings help in achieving the required data safety and performance in a Java application with help of underlying string pool.

Spring Security Siteminder Pre-authentication Example

So far we have learned about securing spring application using login form based security, custom user details security and many more such security related concepts. In this post, I am giving an example of scenario where use is already authenticated via any third party application or tool e.g. site minder …

RESTEasy – Enable Gzip Compression Content Encoding

JAX-RS Resteasy has automatic GZIP decompression support. If the client framework or a JAX-RS service receives a message body with a Content-Encoding of “gzip”, it will automatically decompress it. The client framework automatically sets the Accept-Encoding header to be “gzip, deflate”. So you do not have to set this header …

JAX-RS 2.0 RESTEasy 3.0.2.Final Client API example

JAX-RS 2.0 has brought lots of improvement over previous version. One of major improvement is client API which was completely missing in JAX-RS 1.0. While it was easy to write a portable JAX-RS service, each JAX-RS implementation defined their own proprietary API. JAX-RS 2.0 fills in this gap with a …

[SOLVED] java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder

If you are using RESTEasy 3.x.y which has been support for JAX-RS 2.0, and you are trying to write RESTEasy client then you can face this exception. Full stack trace might look like this: Exception in thread “main” java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:103) at test.jaxrs2.Demo_JAXRS_2_Example.main(Demo_JAXRS_2_Example.java:14) Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder at …

JAXB: Marshal / Unmarshal a Map in Java

We know that JAXB(Java Architecture for XML Binding) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. JAXB mostly is used while implementing webservices or …

JAXB AnnotationsException: 3 counts of IllegalAnnotationExceptions

This exception occurs when you are using JAXB to marshal a Java object (collection type) to xml format. The stack trace looks like this in case of Lists. OR in the case of Maps. Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions java.util.Map is an interface, and JAXB can't …

Securing REST APIs with RESTEasy Filter

Learn to use RESTEasy ContainerRequestFilter to create security filter which is able to to perform authentication and authorization on RESTEasy based web applications.

Java View Bytecode of a Class File

Java compiler rearranges the statements we write in a Java program for performance improvements. Learn to generate the byte code for a class file to see how they have been rearranged.

Java CountDownLatch with Example

Learn the basics of CountDownLatch and how to use it in real-life applications, its methods and use it to control the flow of application.

[SOLVED] Hibernate – No row with the given identifier exists

1. Reason The ObjectNotFoundException exception is thrown when we try to load an entity in hibernate with session.load() method and entity is not found. The exception trace looks like this: 2. Solution The correct way to solve this problem is to use session.get() method. The get() method will return null …

Polymorphism in Java

Polymorphism in Java is the ability to create member functions or fields that behaves differently in different programmatic contexts.

Java Collections Interview Questions

Java collection interview questions. These questions are most asked interview questions for beginners to medium level developers in Java interviews.

Java NIO Path (with Examples)

The Path class, introduced in the Java SE 7 release, is one of the primary entry points of the java.nio.file package. If our application uses Java New IO, we should learn more about the powerful features available in this class. In this Java tutorial, we are learning 6 ways to …

Hibernate Named Query Examples

Learn to create, group and execute named HQL and named native SQL queries in Hibernate using @NamedQuery and @NamedNativeQuery annotations.

Inserting Objects with Hibernate

Learn to insert an object into the database using hibernate’s Session interface, JPA’s EntityManager and HQL Query interface with examples.

RESTEasy Basic Authentication and Authorization Tutorial

Security is an integral part of any enterprise application. Security involves two phases i.e. authentication and authorization. Authentication verifies who you are. Authorization verifies what you are authorized to do. In this post, we will learn to build role based basic authentication/ authorization security for REST APIs. Sections in this …

Apache POI: Read and Write Excel File

Learn to read and write excel files in Java using Apache POI library – with examples. Learn to add and evaluate formula cells and add color formatting also.

Java HashMap Interview Questions

HashMap interview questions and ConcurrentHashMap interview questions. Learn questions like how hashmap works, rehashing in concurrenthashmap and more.

A Comprehensive Guide to SOLID Principles

Learn SOLID principles in Java with examples. Single Responsibility Principle, Open Closed Principle, Liskov’s Substitution Principle, Interface Segregation Principle, Dependency Inversion Principle.

Jersey Hello World Example

This Jersey tutorial will help you set up the Jersey environment in your local machine and get started with a simple Hello World REST API.

RESTEasy Cache control with ETag Example

ETags or entity tags are useful HTTP headers which can help in building a super fast application by minimizing the server load on system. ETag is set to the response to the client so a client can use various control request headers such as If-Match and If-None-Match for conditional requests. …

Java Override toString() with ToStringBuilder

ToStringBuilder is a utility class provided by apache commons lang library. ToStringBuilder is a utility class provided by apache commons lang library. It provides a consistent and better control over what and how much data, an object should expose using toString() method and in which format. It can also help …

Create Windows Executable (.exe) for Java Application

Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executable files. In this post, we will learn making such an executable file for a demo java application. Step1 ) Create a java application I am creating a very basic java class which simply …

RESTEasy Bean Validation using Hibernate Validator Provider

The Bean Validation API (JSR-303) defines a meta-data model and API for bean validation based on annotations. The functionality is achieved through the resteasy-hibernatevalidator-provider component. In order to integrate, we need to add resteasy-hibernatevalidator-provider.jar and hibernate-validator.jar files to the classpath. If you are using maven the, you need to add …

How to Reload Log4j2 Configuration

Excessive logging is a common cause of performance degradation of applications. It is one of the best practices to ensure proper logging within your Java EE application implementation. However, be careful with the logging level that you enable in your production environment. Excessive logging will trigger high IO on your …

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.