Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NoClassDefFoundError: Could not initialize class xxx  RSS feed

 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Could anyone tell me a possible solution to this exception thrown by my server:
java.lang.NoClassDefFoundError: Could not initialize class xxx
 
Harry Hind
Greenhorn
Posts: 1
C++ Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means the server is unable to locate the class file. Just check the classpath.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harry,
I have copied the bean in the right folder which is visible to my application. I think Could not initialize class xxx means it was able to access it. In case the class could not be found, it would display java.lang.NoClassDefFoundError: xxx
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've had this happen to me while using NetBeans and creating war files.

In my situation it is usually something like this:

Project 1 is the servlet and it includes Project 2 which is a class library.

Project 2 depends on a library jar but Project 1 does not need it to compile. So everything seems to work until Project 1 actually calls the class in Project 2 that tries to instantiate the class in the jar.

The solution is to include ALL the libraries included in any sub projects in the servlet's project.

I'm not sure that's your problem but it bites me all the time.

Joe
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:Hi Harry,
I have copied the bean in the right folder which is visible to my application.

Not true. Otherwise you wouldn't get the NoClassDefFoundError.
I think Could not initialize class xxx means it was able to access it.

No, it means it was trying to initialize the class and couldn't. If you click on the NoClassDefFoundError text (it is actually a link to the API), it will tell you why that error is thrown. And it is only thrown for one reason: A class is needed at runtime, but isn't available tot he ClassLoader. For a normal Java application that means it is not in the classpath, and for Web Applications it usually means you didn't include the necessary dependencies in a location required by the application.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

for Web Applications it usually means you didn't include the necessary dependencies in a location required by the application.

Hi Steve,
What do you mean by 'necessary dependencies'? My class does not import other classes.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dependencies for the Web application. This class being one such dependency.

Note this class which can't be found must be in a package if you are using java 1.4 or later and so must be in an appropriate folder structure. So double check package rules are met.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you only asked for a "possible solution" and you got a possible solution. If you're looking for something else, like a solution to an actual problem, it would help if you provided more information. A stack trace would be a good start.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A stack trace would be a good start.

Hi Paul,
Good to hear from you. Here is the stack trace you requested:

[2014-05-31T16:20:45.544+0000] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=17 _ThreadName=http-listener-1(1)] [timeMillis: 1401553245544] [levelValue: 900] [[
StandardWrapperValve[MyServlet]: Servlet.service() for servlet MyServlet threw exception
java.lang.NoClassDefFoundError: Could not initialize class mg.MyClassR
at web.MyServlet.doPost(MyServlet.java:17)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:My class does not import other classes.


From the stack trace it appears that your web.MyServlet class does in fact import a class named mg.MyClassR, and it refers to it at line 17 in the doPost method. The problem is that there is no such class in the servlet's class path, which would include the WEB-INF/classes folder and all jars in the WEB-INF/lib folder of your web application.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem is that there is no such class in the servlet's class path, which would include the WEB-INF/classes folder and all jars in the WEB-INF/lib folder of your web application.

Paul,
the class MyClassR is present in WEB-INF/classes/mg and i have restarted my server several times but it still displays the same error.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Things to check:
1) Case: Java is case sensitive. The file needs to be called MyClassR.class and be in the folder mg. A folder named Mg or MG won't work, neither would myclassr.class. Same goes for the name of the class inside the file.
2) Location: Make sure the WEB-INF you put the file in is the same WEB-INF used to run the server.
3) You are putting the MyClassR.class file in the classpath, and not MyClassR.java right?
4) You put the package statement at the top of the class definition
5) You named the class inside the file exactly as it is shown in the error message

 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve, thanks a lot for your suggestions.

Things to check:
1) Case: Java is case sensitive. The file needs to be called MyClassR.class and be in the folder mg. A folder named Mg or MG won't work, neither would myclassr.class. Same goes for the name of the class inside the file.
2) Location: Make sure the WEB-INF you put the file in is the same WEB-INF used to run the server.
3) You are putting the MyClassR.class file in the classpath, and not MyClassR.java right?
4) You put the package statement at the top of the class definition
5) You named the class inside the file exactly as it is shown in the error message

1.checked. As i said before, it imports MyClassR but fails to initialize it.
2.checked. WEB-INF used to run the server or the application?
3.checked. Everything is ok.
4.Checked. It is done right
5.checked. The name inside the file is MyClassR. Otherwise i think it wouldn't have compiled.
After these checks, the exception is still there.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:1.checked. As i said before, it imports MyClassR but fails to initialize it.
That makes no sense - not sure what you are saying. But no, the class definition is not found. I am not sure why you think it might be, but it isn't.
Paul Ngom wrote:2.checked. WEB-INF used to run the server or the application?
The application. A web application is run in an application server. The server itself probably doesn't have a WEB-INF, but maybe there is a 'global' context WEB-INF. Either way, you shouldn't be using that, you should be using the WEB-INF for the application which is having the problem.

This does lead to another possible cause - related to Joe Areeda mentioned. You have a class loaded from one context (a different application, or some other context) which has a different ClassLoader. You have the class in a WEB-INF which is used for the application context, but not for the same context as the servlet/class which uses it. Web application ClassLoaders can be a bit more complex than desktop applications (for example, see this: http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html). Make sure the class in question is visible in context of the ClassLoader used by the calling class (web.MyServlet).

None of us have access to the information you have related to the application structure. There may be other key points someone can contribute for you to work on, but I think this will end up to you sleuthing around your system to find that important detail you haven't told us and is impossible for use to guess.

Just remember, the reason for that error is always the same thing: that class is not found in the places it is being looked for. You need to find out where it is being looked for, and where it actually exists, then you need to resolve the difference.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
Paul Ngom wrote:1.checked. As i said before, it imports MyClassR but fails to initialize it.
That makes no sense - not sure what you are saying. But no, the class definition is not found. I am not sure why you think it might be, but it isn't.

Oh, I think I get what you mean. You think because your code has import mg.MyClassR at the top of the code, that means, at run time, Java is doing some action - like loading the class definition or something (like an include statement in C or import in python. That isn't how things work in Java. In Java the import statements are used in code at the source, and at compile time to find classes by their short names. Nothing is done with that at run time. So the fact that the error doesn't occur at import mg.MyClassR isn't an indicator of anything at all.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:WEB-INF used to run the server or the application?

Red flag. What do you mean by "WEB-INF used to run the server"?

5.checked. The name inside the file is MyClassR.

Surley you mean MyClassR.class? Right? If so, you should say so. Please be complete and accurate.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In Java the import statements are used in code at the source, and at compile time to find classes by their short names. Nothing is done with that at run time.

Ok Steve. Thanks for the explanation.

Hello Bear,
WEB-INF used to run the server or the application?. I was referring to what Steve posted:

2) Location: Make sure the WEB-INF you put the file in is the same WEB-INF used to run the server.


5.checked. The name inside the file is MyClassR. Here i meant the name of the class in the source file as Steve pointed it out:

5) You named the class inside the file exactly as it is shown in the error message

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!