NotSerializableException in Java with Examples
Last Updated :
05 Jan, 2022
Serialization in Java is a mechanism of writing the state of an object into a byte-stream. It is mainly used in Hibernate, RMI, JPA, EJB, and JMS technologies.
The reverse operation of serialization is called deserialization where byte-stream is converted into an object. The serialization and deserialization process is platform-independent, which means you can serialize an object in a platform and deserialize it on a different platform.
In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface. The exception is thrown by either the serialization runtime, or by the instance of the class. The argument for the NotSerializableException is the name of the class.
The NotSerializableException class extends the ObjectStreamException class, which is defined as the superclass of all exceptions specific to Object Stream classes. Also, the ObjectStreamException class extends the IOException which signals that an I/O exception has occurred.
Illustration:
java.io
Class NotSerializableException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.io.IOException
java.io.ObjectStreamException
java.io.NotSerializableException
Note: All Implemented Interfaces are Serializable interface.
Syntax:
public class NotSerializableException
extends ObjectStreamException
Let us discuss the constructors of this class before a
- NotSerializableException(): Constructs a NotSerializableException object.
- NotSerializableException(String classname): Constructs a NotSerializableException object with message string.
Example 1:
Java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
class Employee {
private String id;
public String getId() { return id; }
public void setId(String id)
{
this .id = id;
}
}
public class GFG {
public static void main(String[] args)
throws IOException
{
FileOutputStream out
= new FileOutputStream( "employee.dat" );
ObjectOutputStream outputStream
= new ObjectOutputStream(out);
Employee obj = new Employee();
obj.setId( "001" );
outputStream.writeObject(obj);
outputStream.close();
}
}
|
Output :
Errors in Code
Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "employee.dat" "write")
at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.base/java.security.AccessController.checkPermission(AccessController.java:897)
at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)
at java.base/java.lang.SecurityManager.checkWrite(SecurityManager.java:752)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:225)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)
at NotSerializableExceptionExample.main(NotSerializableExceptionExample.java:21)
How to deal with the NotSerializableException
- The simplest solution is to find the class that throws the exception and makes it implement the Serializable interface. However, this may not be feasible if the class that throws the exception belongs to a third-party library.
- In case the class refers to non-serializable objects and these objects should not be serialized, then, you can declare these objects as transient. Once a field of a class is declared as transient, then, it is ignored by the serializable runtime.
Example 2:
Java
import java.io.Serializable;
class Student implements Serializable {
int id;
String name;
public Student( int id, String name)
{
this .id = id;
this .name = name;
}
}
class Persist {
public static void main(String args[])
{
try {
Student s1 = new Student( 007 , "Test" );
FileOutputStream fout
= new FileOutputStream( "TestFile.txt" );
ObjectOutputStream out
= new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
out.close();
System.out.println(
"Object stored successfully" );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
Object stored successfully
Share your thoughts in the comments
Please Login to comment...