& Quot; weird & quot; ClassNotFoundException

Source: Internet
Author: User

When I wrote a project today, I found that I introduced all the JAR files I needed. Why does it prompt ClassNotFoundException?

I guess:

Don't I have this package?

I found the class in this JAR according to the tree structure. Oh, no, I went on to find another reason.

Is my JAR in conflict with the JDK version?

I used JDK1.7 at the time, so I changed JDK1.6.

Is this JAR dependent on another JAR?

I used the decompilation tool to compile the JAR and found that the inherited or introduced packages can be found in this project. That's not the reason. What would it be?

Inadvertently, I copied all the JAR files to the project's web-inf/lib and ran the program. What's going on?

In general, it is related to classLoader.Pure java Project, It does not exist in the WEB-INF directory, so the jar package is generally introduced directly through buildpath, for example, I want to introduce Spring3X, then first define a user library, and then introduce through build path.

Java ProjectIf the local JRE is used, the classLoader is separated when loading jar and class. For the class we compile, it will be under APP_HOME/bin. The imported jar package or user library configuration information will appear in the APP_HOME/. classpath file, and the ClassLoader will intelligently load these classes and jar .. The content of the classpath file is as follows:

 
 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <classpath> 
  3.     <classpathentry kind="src" path="src"/> 
  4.     <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre6"/> 
  5.     <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Spring3.1.0"/> 
  6.     <classpathentry kind="lib" path="E:/Jar_Framework/cglib/cglib-2.2.2.jar" sourcepath="E:/Jar_Framework/cglib/cglib-src-2.2.2.jar"/> 
  7.     <classpathentry kind="lib" path="E:/Jar_Framework/commons-logging-1.1.1-bin/commons-logging-1.1.1/commons-logging-1.1.1.jar"/> 
  8.     <classpathentry kind="lib" path="E:/Jar_Framework/aopalliance-1.0/aopalliance-1.0.jar"/> 
  9.     <classpathentry kind="lib" path="E:/Jar_Framework/cglib/cglib-nodep-2.2.2.jar"/> 
  10.     <classpathentry kind="lib" path="E:/Jar_Framework/spring revelant/aspectj-1.6.12.jar"/> 
  11.     <classpathentry kind="lib" path="E:/Jar_Framework/spring revelant/aspectjweaver-1.6.8.jar"/> 
  12.     <classpathentry kind="lib" path="E:/Jar_Framework/spring revelant/asm-3.2.jar"/> 
  13.     <classpathentry kind="output" path="bin"/> 
  14. </classpath> 

 

In this way, the ClassLoader will find all the required classes correctly.

ForJava web project, It is different, although eclipse's workspace still has. classpath file, but even if you import your own user library, it will not appear in. in classpath, this is the key to the problem. Why?

For java web projects, it is deployed to web servers, such as Tomcat, Weblogic, and WebSphere, instead of running through local JRE. These servers all implement their own class loaders.

Take the typical Tomcat result as an example. The four groups of directory structures: common, server, shared, and webapps correspond to four different custom class loaders: CommonClassLoader, CatalinaClassLoader, SharedClassLoader, and WebappClassLoader,The WebappClassLoader loader is responsible for loading the class libraries under the WEB-INF of various web projects under webapps.However, the jar package we introduced through the user library will not be loaded by the WebappClassLoader loader, so ClassNotFoundException will inevitably be reported.

In addition, if you want to export an Excel file, you can add response. setHeader). Do you want to open it in a browser.

 
 
  1. response.setHeader("Content-Disposition", "attachment;filename=" 
  2.  
  3.                 + fileName); 

When making a mistake, you must be careful when making such a low-level mistake. Pay attention to it later...

This article is from the "On My Way" blog, please be sure to keep this source http://shuyangyang.blog.51cto.com/1685768/1020025

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.