Blog |

How to Fix the Type Not Present Exception in Java

How to Fix the Type Not Present Exception in Java
Table of Contents

The TypeNotPresentException is a runtime exception in Java that is thrown when an application attempts to access a type using a string that represents the name of the type, but the definition for the type with the specified name cannot be found. It differs from ClassNotFoundException as ClassNotFoundException is a checked exception, whereas TypeNotPresentException is unchecked.

Since the TypeNotPresentException is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor.


What Causes TypeNotPresentException

The TypeNotPresentException is thrown when the definition of a type, attempted to be accessed using a specified name, cannot be found. The application attempts to access the type using a string that represents the name of the type, but no definition for the type with the specified string can be found.

This exception can be thrown when undefined type variables are attempted to be accessed as well as when types (classes, interfaces or annotation types) are loaded. It can, in particular, be thrown by the API used to read annotations reflectively.


TypeNotPresentException Example

Here's an example of a TypeNotPresentException thrown when a custom annotation contains an element that refers to a class that exists at compile-time but not at runtime. When this annotation is used by a class and read reflectively, a TypeNotPresentException is thrown.

First, the custom annotation MyAnnotation is created. This annotation contains an element context that refers to the javax.xml.bind.JAXBContext class:

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.xml.bind.JAXBContext;

public @interface MyAnnotation {
    Class<JAXBContext> context() default JAXBContext.class;

Next, a class TypeNotPresentExceptionExample is created that uses the MyAnnotation annotation. It then uses reflection to read the annotation and it’s context element:

public class TypeNotPresentExceptionExample {
    public static void main(String[] args) {

Since the context element refers to the javax.xml.bind.JAXBContext class that exists at compile-time but not at runtime, running the above code throws the TypeNotPresentException:

Exception in thread "main" java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext not present
    at java.base/java.lang.reflect.Method.getDefaultValue(
    at java.base/sun.reflect.annotation.AnnotationType.<init>(
    at java.base/sun.reflect.annotation.AnnotationType.getInstance(
    at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotation2(
    at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations2(
    at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations(
    at java.base/java.lang.Class.createAnnotationData(
    at java.base/java.lang.Class.annotationData(
    at java.base/java.lang.Class.getAnnotation(
    at TypeNotPresentExceptionExample.main(
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBContext
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
    at java.base/java.lang.ClassLoader.loadClass(
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(
    at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(
    at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(
    at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(
    at java.base/sun.reflect.annotation.AnnotationParser.parseSig(
    at java.base/sun.reflect.annotation.AnnotationParser.parseClassValue(
    at java.base/sun.reflect.annotation.AnnotationParser.parseMemberValue(
    at java.base/java.lang.reflect.Method.getDefaultValue(
    ... 9 more


How to Fix TypeNotPresentException

To fix the TypeNotPresentException, the libraries that contain the missing type (class, interface or annotation) should be added to the application runtime dependencies. The versions of these libraries should also be checked to ensure they are compatible at compile-time and runtime. The project should then be cleaned and compiled from scratch, and the application server restarted.

It's also helpful to run the application with the -verbose: class option to check the loaded classes and see whether the type is loaded at runtime or not.


Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape