java.lang.ClassFormatError : Absent Code attribute in method that is not native or abstract in class file …

Problem

A very strange and rare problem, happened in JPA or Hibernate development.


Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is 
        not native or abstract in class file javax/persistence/GenerationType
	
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(Unknown Source)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.access$000(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknow

Solution

This is always caused by the javaee.jar which is located at Java.net. Many developers like to grab the javaee.jar with the following Maven coordinate :


  <repositories>
  	<repository>
  		<id>Java.Net</id>
  		<url>http://download.java.net/maven/2/</url>
  	</repository>
  </repositories>
 
  <dependencies>
    <!-- Javaee API -->
	<dependency>
    	<groupId>javax</groupId>
    	<artifactId>javaee-api</artifactId>
    	<version>6.0</version>
	</dependency>
  </dependencies>

But, the javaee.jar from java.net is not contains any method bodies, but the APIs name only. Which is not suitable to use for running or deploying along with your application.

The good practice is always get the original full version of javaee.jar file from the http://java.sun.com/javaee/. Just download and install the J2EE SDK, and the javaee.jar can be found in the “\J2EE_SDK_FOLDER\lib” folder. Include it into your local Maven repository or poject classpath will get rid of the above error message.

Reference

  1. http://weblogs.java.net/blog/ludo/archive/2007/01/java_ee_5_apis.html
  2. http://forums.java.net/jive/message.jspa?messageID=226931
  3. http://jersey.576304.n2.nabble.com/Absent-Code-attribute-in-method-that-is-not-native-td2632542.html

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Swarndeep Singh
Guest
Swarndeep Singh

Getting exception while creating client. jersey-core-1.13.jar and jersey-client-1.13.jar are included as Maven dependency.

Client c = Client.create ();

gives:

Exception in thread “main” java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/MediaType
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)

Please help!

Krishnan Unni
Guest
Krishnan Unni

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.filter.CharacterEncodingFilter]: Factory method ‘characterEncodingFilter’ threw exception; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
… 42 common frames omitted
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException

Krishnan Unni
Guest
Krishnan Unni

Can you help with this error ?

faruk
Guest
faruk

thank you, again again.

Vishnu Sarasan
Guest
Vishnu Sarasan

It’s working fine now. Thank You.

Usman
Guest
Usman

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2854)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1159)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:108)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:58)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:297)
at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:1078)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:261)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4612)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

I am having this issue i am not using maven… please help me hwo to fix this problem

Jesper Tejlgaard
Guest
Jesper Tejlgaard

Hi

Anamika Mishra’s approach solves problems with applications installed on a Java EE server, but it does not solve the problem for tests executed during the Maven lifecycle, because dependencies in scope provided are available at the test runtime.

For those experiencing this problem during tests there is another solution: https://community.jboss.org/wiki/WhatsTheCauseOfThisExceptionJavalangClassFormatErrorAbsentCode

I favour this over having the full javaee.jar as part of my final application archive (war/ear)

paul brickell
Guest
paul brickell

Seriously? That really makes unit testing my ‘POJOs’ really hard.

Radu
Guest
Radu
I have got the folow error: Exception in thread “main” java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ejb/EJBException I’ve added the following in the pom.xml file and its work. I hope to be helpful. javax.ejb ejb-api provided javaee javaee-api provided
ClintJCL
Guest
ClintJCL

I’d like to know how to see if my JAR is an API only. It has .class files but is there a way to really tell? Do you have the filesize for the 2 different versions?

Guest
Guest
Guest

2 years after leaving my comment here, I still don’t know how to fix the error, because I never got a response to my question. Sigh.

omile
Guest
omile
please i need help in developing standalone application using EJB. first i thank all who have been contributing in java.i am new to EJB. my problem is :i tried to build remote application using Ejb , 1. i build entity bean from datebase 2. i tried to build session bean from entity class for remote AP. then error=”EJB Remote Interface depends on JPA AEntity classes. JPA Entities are inaccessible if they are located in mode.” i used netbean 7.2.1. as i was making a research over a week ,i stormble over this solution 1 .that i should build class library 2. create entity bean in the class library 3.add the class library to the already created ejeModule… Read more »
meursaul
Guest
meursaul
Hi, My name is Meursaul, first excuse me for my English because it is not good. Thank you mkyong and thank you all man who contribute to this tutoriel, it is helpfull. I try to execute the example where you used spring+hibernate+struts and maven and i have this error: ———————————————————————— [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:run (default-cli) on project aaaa: Execution default-cli of goal org.codehaus.mojo:tomcat-maven-plugin:1.1:run failed: An API incompatibility was encountered while executing org.codehaus.mojo:tomcat-maven-plugin:1.1:run: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/http/HttpServlet [ERROR] —————————————————– [ERROR] realm = plugin>org.codehaus.mojo:tomcat-maven-plugin:1.1 [ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy… Read more »
anamika mishra
Guest
anamika mishra

Thanks mkyong that surely explains the issue in details. Another way of solving this could be using the maven scope as provided i.e.

 <scope>provided</scope> 

. This ensures that the api only jar is used only for compilation and not at run time. Place the implementation jar in the server lib.

trackback
java.lang.ClassFormatError: Absent Code attribute in method …
naveed khan
Guest
naveed khan

Sorry but i dont know how to add above dependencies in project..

Jacek
Guest
Jacek

Your directions are very helpfull.
Many thanks for that.
ps.
java.net make big mess whit his libraires – they are in NetBeans IDE.

trackback
Hibernate Tutorials

[…] java.lang.ClassFormatError : Absent Code attribute in method that is not native or abstract in class… […]

peterborough hair dresser
Guest
peterborough hair dresser

I was curious if you ever considered changing the page layout of your site?
Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people
could connect with it better. Youve got an awful lot of
text for only having one or two pictures. Maybe you could
space it out better?