Tampilkan postingan dengan label debugging. Tampilkan semua postingan
Tampilkan postingan dengan label debugging. Tampilkan semua postingan

Sabtu, 19 Januari 2013

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 �

Selasa, 25 Desember 2012

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 Java
Solving 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.

Jumat, 21 Desember 2012

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 almost everything starting from 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 the 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 �

Sabtu, 15 Desember 2012

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 do you get this error and from where does 'javac' is not recognized as an internal or external command comes. This is a 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 the following command :

# echo %PATH%

If you see your JDK installation folder or JAVA_HOME in PATH and included bin directory which contains all java binaries including javac and java commands which are 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 �

Kamis, 13 Desember 2012

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 a class at runtime using Reflection which throws ClassNotFoundException if the 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 �