Understanding Tomcat Classpath-FAQs and Solutions

Source: Internet
Author: User
In many ApacheTomcat user forums, a frequently asked question is how to configure Tomcat classpath so that a web application can find classes or jar files to work properly. Just like many problems that plague new Tomcat users, this problem can be easily solved. In this article, we will introduce In many Apache McAt user Bbs'Target = '_ blank'> ForumA problem is often raised, that is, how to configure Tom CatSo that a web application can find the class or jar file to work normally. Just like many problems that plague new Tomcat users, this problem can be easily solved. In this article, we will introduce how Tomcat generates and utilizes classpath, And then solve most common problems related to classpath one by one.
Why does Classpaths cause trouble to Tomcat users?
A classpath is a parameter that tells the Java Virtual Machine where to find classes and packages to run a program. Classpath is always set outside the program source code. separating it from the program allows java code to reference classes and packages in an abstract way, allowing the program to be configured on any system. Why are experienced java users already very clear about what classpath is, but they still encounter such problems when running the program in Tomcat?
There may be three reasons: 1. Tomcat processes classpaths in a different way than other java programs. 2. The classpaths processing method may vary depending on the Tomcat version. 3. Tomcat documents and default configurations must be completed in a specific way. If you do not follow this method, you will be in trouble. There is no information to provide about how to solve common classpath problems, such as external dependencies, shared dependencies, or identical dependencies of multiple versions.
How does Tomcat Classpath differ from the standard Classpath?
Apache Tomcat is designed to be as independent, intuitive, and automated as possible. In order to effectively manage and standardize the web application configuration and deployment process, for the sake of security and naming controls, restrict access to different databases. This is why the java classpath environment variable is not used. java classpath is the default place for declaring dependency class libraries. Tomcat's start script ignores this variable, when you create a Tomcat classloader, your classpaths is generated.
To understand how Tomcat handles classpath, take a look at the Startup Process of Tomcat 6 below: 1. boots of Java Virtual Machine TrThe ap loader loads the java core class library. The Java virtual machine uses the JAVA_HOME environment variable to locate the location of the core library. 2. Startup. sh, use the start parameter to call Catalina. sh, override the classpath of the system, and load bootstrap. jar and tomcat-juli.jar. These resources are only visible to Tomcat. 3. For each deployed Cont ExT create Class loader, load all classes and jar files located under each web application WEB-INF/classes and WEB-INF/lib directory. Each web application can only view resources in its own directory. 4. The Common class loader loads all classes and jar files located in the $ CATALINA_HOME/lib directory. These resources are visible to all applications and Tomcat. How does Tomcat's class loading mechanism change between different versions?
In the previous Tomcat version, its class loading mechanism is somewhat different.
In Tomcat 4.x and earlier versions, a server loader is responsible for loading the Catalina class, And now commons loader is responsible for this. Tomcat 5.x, A shar EdLoader is responsible for loading classes shared between applications. These classes are located in $ CATALINA_HOME/shared/lib. In Tomcat6, this method is canceled. Tomcat 5.x also includes a Catalina loader, which loads all Catalina components and is now replaced by Common loader.
What should you do when you cannot do things as required by Tomcat?
If you use the methods recommended in the Tomcat documentation, you should not have any questions about classpath. Your wars contains copies of all libraries and packages. You have no reason to share a jar file among multiple applications. You do not need to call any external resources, you will not encounter complex situations, such as the need for multiple versions of a jar file when a web application is running. However, if you really cannot follow the recommended method, a file can solve all your problems: catalina. properties.
Use catalina. properties to configure Tomcat Classpath
For users who do not want to use the default loading method, fortunately, Tomcat's classpath options are not hard-coded and they are from $ CATALINA_HOME/conf/catalina. properties file.
This file contains all the loaders except bootstrap and system loader. Check this file and you will find some new discoveries: 1. Server and Shared loader have not been deleted. If their attributes are not defined, Commons loader is responsible for processing them. 2. Classes and jar files loaded by various loaders are not automatically loaded. They are only specified as a group using a simple wildcard syntax. 3. You cannot specify an additional warehouse. In fact, you can.
Server loader should not be changed, but shared loader still has many useful applications. (Shared loader loads its class at the final stage of the startup process after Commons loader ). Now let's take a look at some common problems and how to solve them.
Problems, solutions and best practices
Problem: My application depends on an external repository and I cannot reference it. Let Tomcat know an external warehouse. In the shared loader location of the catalina. properties file, use the correct syntax to declare this warehouse. The syntax is based on the type of the file or repository you want to configure: 1. Add a folder as the class repository and use "path// FoldErname" 2. Add all jar files in a folder as the class repository and use "path/to/foldername/*. jar" 3. Add a single jar file as a class repository and use" File: // Path/to/foldername/jarname. jar" 4. Call environment variables in the format of $ {}, for example, $ {VARIABLE_NAME} 5. Declare multiple resources, separated by commas 6. All paths are relative to CATALINA_BASE, CATALINA_HOME, or absolute paths.
Problem: I want multiple applications to share a jar file, which is in Tomcat. Except for some common third-party libraries (such as JDBC drivers), it is best not to include additional libraries in the $ CATALINA_HOME/lib directory, even if this is feasible in some cases. Re-create directories such as/shared/lib and/shared/classes, and configure the shared. loader attribute in catalina. properties: "Shared/classes, shared/lib/*. jar"
Problem: Except for another framework, I used an embedded Tomcat server in an application and encountered classpath errors when I accessed the framework components. This question seems to be beyond the scope of this article, but as a common classpath-related question, I would like to give a brief introduction to how to cause your error. When embedded into an application that contains another core framework (Wicket or Spring), Tomcat uses System classloader to load the core class when starting the framework, instead of loading from the WEB-INF/lib directory of the application. Java class loading is a lazy load, that is, the first loader requesting a class has the lifecycle of the class. If the System classloader class is invisible to web applications, the Framework-related classes are loaded first, and the Java Virtual Machine will prevent other instances from being created, this causes the classpath error. One way to solve this problem is to add a custom bootstrap classloader so that the classloader can load the appropriate library and start the rest of the program normally.
Problem: I am using a standard application. The war of the program contains all dependent packages, but I still encounter class definition errors. This problem may be caused by many things, including the incorrect compilation or deployment process, but it is most likely that the directory structure of the web application is incorrect. Java name conversion requires that the class name be mapped to the directory structure where the class is stored. For example, a class com. mycompany. mygreat. class needs to be stored in the directory WEB-INF/classes/com/mycompany /. Frequent loss of a dot in the code will cause classpath problems.
Problem: different modules of my web application need to use two different versions of the same jar package. This often occurs when multiple web frameworks are used in an application, which depend on different versions of a library. There are several solutions, but they are not related to classpath. Here we will talk about this problem because some users try to solve this problem by specifying different versions of classpath of the dependent library in the Manifest file of the jar file of the framework. This method is supported in some web application servers, so some users want to use this method in Tomcat. However, specifying classpath in the Manifest file is not supported in Tomcat, which is not part of the Servlet description. There are four ways to solve this problem: 1. You can update the Framework version. if the version is the same as that of other frameworks. 2. You can create two or more custom classloaders, one for each jar file, in WEB/INF/context. configure in the xml file to create two instances of classes of different versions you need. 3. You can use the jarjar tool to package the framework and the libraries it depends on into a jar file. Then they will be loaded together. 4. If you find that you need to handle this situation every other day, you should consider using the OSGI framework, this framework has many methods specifically used to handle the situation that multiple versions of a class need to run in the same Java virtual machine.
Best practices 1. Avoid using Commons loader in Tomcat to load libraries and packages that are not released according to Tomcat standards, which may cause compatibility errors. If you need to share a library or package among multiple applications, create the shared/lib and shared/classes directories and configure them to the Shared loader in the catalina. properties file. 2. A common third-party shared library, such as JDBC driver, is an exception to the above rules. These should be placed in the $ CATALINA_HOME/lib directory. 3. If possible, use Tomcat as recommended by Tomcat developers. If you find that you have to configure classpath frequently, you may need to reconsider your development process.
Related Article

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.