Showing posts with label debugging. Show all posts
Showing posts with label debugging. Show all posts

Sunday 25 August 2013

Top Most How to fix java.io.NotSerializableException: org.apache.log4j.Logger Error in Java

java.io.NotSerializableException: org.apache.log4j.Logger error says that 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 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 staticor transientand 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 default serializabtion process, either make it transient or static. Making it static final is preferred option due to many reason 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 neither constructornot instance initializer block is called during deserialization. By making it static and final you ensure that its thread-safeand all instance of Customer class can share same logger instance, By the way this error is also one of the reason Why Logger should be declared static and finalin Java program. Just make 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 cause 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 Logger static and final.

Other Serialization and troubleshooting articles from Javarevisited Blog

Wednesday 21 August 2013

Top Most Invalid initial and maximum heap size in JVM - How to fix

I was getting "Invalid initial heap size: -Xms=1024M" while starting JVM and even after changing maximum heap size from 1024 to 512M it keep crashing by telling "Invalid initial heap size: -Xms=512m , Could not create the Java virtual machine" , I check for almost everything starting form checking how much physical memory my machine has to any typo in JVM parameters, only to find out that instead of M, I had put MB there. Java accepts both small case and capital case for Kilo, Mega and Gigs. you can use m or M, g or G etc but never used MB, GB or KB.  Similar problem can occur with maximum heap size specified by -Xmx. Also from Java 6 update 18 there is change on default heap size in JVM.  
Read more »

Monday 19 August 2013

Top Most How to fix Failed to load Main-Class manifest attribute from jar - Java Eclipse Netbeans Tutorial

If you have tried creating JAR file and running Java program form command line you may have encountered "Failed to load Main-Class manifest attribute from jar" , This error which haunts many Java programmer when they enter into command line arena and tries to create JAR file in command lineor IDE like Netbeans and Eclipse. Major problem with "Failed to load Main-Class manifest attribute from jar" error is that, it’s unnecessary creates panic which it doesn't deserve, may be because process involves of creating and running JAR is not that common like running Java program using "java" command. Anyway in this Java tutorial we will see Why  "Failed to load Main-Class manifest attribute from jar" comes and How to fix  Failed to load Main-Class manifest attribute from jar error.
Read more »

Thursday 15 August 2013

Top Most 'javac' is not recognized as an internal or external command - cause and solution

So you are trying to compile your Java source file and getting "'javac' is not recognized as an internal or external command". If this is your first Java program or HelloWorld than I suggest to go through How to compile and run HelloWorld in Java because that explains what do you need before you compile and run any Java program. If you have crossed that level and knows about How to set PATH in Java then there is something wrong while setting PATH in Java. Anyway let's see when does you get this error and from where does 'javac' is not recognized as an internal or external command comes. This is an standard error in Windows command line when you type a command which is not available in System PATH, here javac command which is used to compile Java source file and produces class files are not in PATH. Best way to verify this is executing following command :

# echo %PATH%

If you see your JDK installation folder or JAVA_HOMEin PATH and included bin directory which contains all java binaries including javac and java commands which is used to compile and run Java program. Most likely your PATH may not have JDK/bin in PATH, if that's the case just include bin folder of JDK in your PATH. See how to set PATH for Java in Windows for step by step guide.
Read more »

Tuesday 13 August 2013

Top Most java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver - cause and solution

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver Exception comes when you try to connect Oracle database from Java program and Oracle driver is not available in Classpath. We have already seen How to connect Oracle database from Java program and found that, In order to connect to Oracle database, we load and register driver using Class.forName("oracle.jdbc.driver.OracleDriver") and this code loads class at runtime using Reflection which throws ClassNotFoundException if class in question e.g. "oracle.jdbc.driver.OracleDriver" is not found. I have already listed down various reason of ClassNotFoundExcepiton in Java,which is also applicable in this case. By the way java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver is similar to  java.lang.ClassNotFoundException: mysql.jdbc.driver.MySQLDriver which comes if you try to connect MySQL database from Java program and corresponding driver is not in your classpath. You can refer link for getting more ideas which can be used to fix java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver error as well.
Read more »

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Labels

Core Java programming core java interview question Core Java Faq's Servlets coding database jsp-servlet spring Java linux unix interview questions java investment bank Web Services Interview investment bank mysql Senior java developer interviews best practices java collection tutorial RMI SQL Eclipse FIX protocol tutorial tibco J2EE groovy java questions SCJP grails java 5 tutorial jdbc beginner error and exception Design Patterns Java Programming Tutorials fundamentals general object oriented programming xml Java Programs Hibernate Examples Flex JAMon Java xml tutorial logging Jsp Struts 2.0 Sybase and SQL Server debugging java interviews performance FIX Protocol interview questions JUnit testing WebSphere date and time tutorial experienced java IO tutorial java concurrency thread Ejb Freshers Papers IT Management Java Exapmle Java Script SQL and database tutorial examples Scwcd ant tutorials concurrency example and tutorial future state homework java changes java threading tricky Agile Business of IT Development JSTL Java JSON tutorial Java multithreading Tutorials PM Scrum data structure and algorithm java puzzles java tips testing tips windows 8 5 way to create Singleton Object Architect Interview Questions and Answers Architecture Architecure Bluetooth server as swing application that searches bluetooth device in 10 meter circle and show all devices. You can send file to any bluetooth device. C Programming CIO Callable Statement in Java Circular dependency of Objects in Java Comparable Example in Collection Custom annotation in Java Developer Interview Divide and rule example in java Drupal Example of Singleton Pattern FIX protocol ForkJoin Example in Java 7 Get data from dynamic table with Java Script Git HTML and JavaScript Health Hello World TCP Client Server Networking Program Hibernate Basics Hibernate Interview Question Answer J2EE Interview Question And Answers J2ME GUI Program JEE Interview QA JMS interview question Java J2EE Hibernate Spring Struts Interview Question Java System Property Java Threads Manager Portlets Provident Fund Read data from any file in same location and give the required result. Reading Properties File in Java Redpoint Rest WebService Client Rest Webservice Test SAL join with ven diagram SCP UNIX COMMAND SSL Singleton Pattern in Java Spring Bean Initialization methods and their order Spring Interview Questions Struts Struts 2.0 Basics Struts 2.0 Design Pattern Submit Html Form With Java Script On The Fly Unix executable For Java Program XOM DOM SAX XP books computers core java; core java; object oriented programming data structure; java investment bank; design pattern dtd duplicate rows in table get browser name with jquery grails podcast inner class java beginners tutorial java cache java networking tutorial java spring java util; java collections; java questions java.java1.5 linked list mailto function with all browser oracle database oracle duplicate rows orm schema social spring mvc questions struts transaction tricks tweet windows xslt