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;
@Retention(RetentionPolicy.RUNTIME)
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:
@MyAnnotation
public class TypeNotPresentExceptionExample {
public static void main(String[] args) {
TypeNotPresentExceptionExample.class
.getAnnotation(MyAnnotation.class)
.context();
}
}
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(Method.java:680)
at java.base/sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:132)
at java.base/sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:267)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:121)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:73)
at java.base/java.lang.Class.createAnnotationData(Class.java:3886)
at java.base/java.lang.Class.annotationData(Class.java:3875)
at java.base/java.lang.Class.getAnnotation(Class.java:3780)
at TypeNotPresentExceptionExample.main(TypeNotPresentExceptionExample.java:5)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBContext
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:427)
at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at java.base/sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:440)
at java.base/sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:421)
at java.base/sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:350)
at java.base/java.lang.reflect.Method.getDefaultValue(Method.java:672)
... 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
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!