Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

How to fix java.io.NotSerializableException: org.apache.log4j.Logger Error in Java? Example

java.io.NotSerializableException: org.apache.log4j.Logger error says that an instance of org.apache.lo4j.Logger is not Serializable. This error comes when we use log4j for logging in Java and create Logger in a Serializable class e.g. any domain class or POJO which we want to store in HttpSession or want to serialize it. As we know from 10 Java Serialization interview question that, if you have a non serializable class as member in a Serializable class, it will throw java.io.NotSerializableException Exception.

Look at the below code :

public class Customer implements Serializable{

private Logger logger =  Logger.getLogger(Customer.class)



If the instance of Customer will be stored in HttpSession or Serialized externally it will throw "java.io.NotSerializableException: org.apache.log4j.Logger" because here logger instance is neither static or transient and it doesn't implement Serializable or Externalzable interface.

How to solve java.io.NotSerializableException: org.apache.log4j.Logger

How to fix java.io.NotSerializableException: org.apache.log4j.Logger Error in JavaSolving java.io.NotSerializableException: org.apache.log4j.Logger  is simple, you have to prevent logger instance from the default serialization process, either make it transient or static. Making it static final is preferred option due to many reasons because if you make it transient than after deserialization logger instance will be null and any logger.debug() call will result in NullPointerException in Java because of neither constructor not instance initializer block is called during deserialization. 

By making it static and final you ensure that its thread-safe and all instance of Customer class can share the same logger instance.

By the way this error is also one of the reasons Why Logger should be declared static and final in Java program. Just make the following code change to fix java.io.NotSerializableException: org.apache.log4j.Logger in Java.

public class Customer implements Serializable{

private static final Logger logger =  Logger.getLogger(Customer.class)



That's all on how to fix java.io.NotSerializableException: org.apache.log4j.Logger in Java. We have seen what caused java.io.NotSerializableException: org.apache.log4j.Logger, it's because Logger class is not Serializable but we also learned that there is no point serializing Logger instance and better to make The logger is static and final.

Other Serialization and troubleshooting articles from Javarevisited Blog


Senthil Kumar said...


I am also facing same issue only in Cluster environment.

public class ViewAction implements Serializable{

private static Logger LOGGER = Logger.getLogger(ViewAction.class);


java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.MarshalException: CORBA BAD_PARAM 0x4f4d0006 Maybe; nested exception is:
java.io.NotSerializableException: org.apache.log4j.Logger is not serializable
at com.ibm.CORBA.iiop.UtilDelegateImpl.wrapException(UtilDelegateImpl.java:749)
at javax.rmi.CORBA.Util.wrapException(Util.java:296)
at com.cib.infra.txn._TxnProcessorRemote_Stub.handleRequest(Unknown Source)
at com.ws.framework.PortletAction.connect(PortletAction.java:461)
at com.ws.framework.PortletAction.executeHostRequest(PortletAction.java:435)
at com.ws.view.action.ViewAction.getExportData(ViewAction.java:294)
at com.ws.services.exportdata.ExportServiceServlet.doPost(ExportServiceServlet.java:141)
at com.polaris.iportal.ws.services.exportdata.ExportServiceServlet.doGet(ExportServiceServlet.java:335)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)

Javin @ Java Classloder Working said...

for which class, it it for this class then you already made Logger instance static. I think you should check ExportServiceServlet.java

Senthil Kumar said...

in my ExportServiceServlet.java it declared like below one.

public class ExportServiceServlet extends HttpServlet
private static Logger logger = Logger.getLogger(com/ws/services/exportdata/ExportServiceServlet);

Javin @ Must Override Eclipse Error said...

That's also static. Can you let me know when does this error occur, I mean which object are you trying to store in Session? Another way is look for a logger instance which is not static.

Senthil Kumar said...

Thanks for Suggestion...

In PortletAction class logger instance is not static. Will this cause issue?

Also Is that required to declare it like "private static final" or "private static" is enough...

public abstract class PortletAction extends IOrbiAction
public PortletAction()
logger = Logger.getLogger(com/ws/framework/PortletAction);
private Logger logger;


Javin @ pass by value or reference said...

Yes, this will cause issue, if you try to put any object, which directly or indirectly has reference of this class. Make it private static. Regarding your question, its always good to made static variable final, to avoid accidental change, because it's shared between instances. to avoid java.io.NotSerializableException, its enough to making it static.

Anonymous said...

you helped me a lot ... thank you

Unknown said...

Hi I'm encountering similar intermittent error java.io.NotSerializableException: oracle.jdbc.driver.T4CConnection. Could you suggest what approach could be taken for the same.Could the above approach of making private static would help in this case

javin paul said...

Hello @Unknown, yes you can either make it static or transient, both will work.

Unknown said...

@Javin Paul: Hi, when i try to make a remote ejb call to the bean , it seems to throw java.io.NotSerializableException: oracle.jdbc.driver.T4CConnection. tried making connection object private static. but did not help. The environment is Jboss eap 7(wildfly)

javin paul said...

Check your EJB bean, does it contain any field which is not serializable and not transient or static?

Post a Comment