SoFunction
Updated on 2025-03-03

Solution to the JAR package error when Tomcat starts Invalid byte tag in constant pool exception

Preface

When developing Java Web applications, we often use Apache Tomcat as a Servlet container for deployment and testing. However, during project startup, you sometimes encounter exceptions similar to "Invalid byte tag in constant pool", which are often associated with Tomcat's parsing incompatible with certain JAR packages, especially when different versions of JDK and JAR packages are introduced into your project.

This article will analyze the causes of this problem in detail and explore several effective solutions. Through this article, readers can better understand dependency management in Java projects and master the skills to deal with JAR package conflicts and Tomcat compatibility issues.

Problem description

When starting Tomcat, you may encounter the following error message:

Unable to process Jar entry [META-INF/versions/9/] from Jar [jar:file:/path/to/your/jar/file] for annotations
: Invalid byte tag in constant pool: 19
	at (:127)
	...
	at (:5058)
	...

From the log, the error comes fromInvalid byte tag in constant pool: 19, and it is withFile related. The problem is focused on Tomcat dealing withcos_api-bundle-5.6.I encountered content that could not be parsed.

Analysis of the causes of problems

This exception message prompts us that Tomcat encountered an incompatible bytecode format when processing annotations in the JAR package. Specifically, this is usually caused by files in the JAR package. It is part of a module system introduced in Java 9 to define a modular project structure. If your project is using Java 8 or earlier, Tomcat will throw an exception because it cannot recognize this class file.

Here are a few of the main reasons for this problem:

  1. JAR package version is incompatible: A JAR package compiled based on Java 9 or later was introduced, but the JDK used by the project or Tomcat is Java 8 or earlier. In this case, Tomcat cannot handle Java 9's bytecode format.

  2. Tomcat version is too old: Some earlier Tomcat versions (such as Tomcat or earlier) do not fully support Java 9 or newer bytecode specifications, especially when dealing withhour.

  3. Dependency conflict: There may be multiple different versions of JAR packages in the project, which may be compiled from different Java versions, which can also cause compatibility issues.

Solution

In response to the above problems, we can start to solve them from the following aspects.

1. Upgrade Tomcat or JDK version

The most straightforward solution is to make sure your development environment matches the dependent JAR package. If you are using Java 9 or later JAR packages, you should:

  • Upgrade your Tomcat to the latest version that supports Java 9 or later. Generally, Tomcat and later have good support for Java 9+.
  • Check if your project uses the lower version of JDK. If yes, it is recommended to upgrade the JDK to at least Java 11 to ensure compatibility.

After upgrading, restart Tomcat to see if the problem persists. If the problem is solved, it means that it is due to compatibility between the Java version and the Tomcat version.

2. Exclude unwanted JAR packages or versions

If you don't need the JAR packageFiles, or these class files have no practical purpose for your project, you can choose to exclude them in the build configuration file. For example, when using Maven or Gradle to manage dependencies, you can exclude incompatible modules.

Exclude in Maven:

<dependency>
    <groupId></groupId>
    <artifactId>cos_api-bundle</artifactId>
    <version>5.6.35</version>
    <exclusions>
        <exclusion>
            <groupId>module</groupId>
            <artifactId>module-info</artifactId>
        </exclusion>
    </exclusions>
</dependency>

By excluding unwanted modules, you can avoid Tomcat processing these files at startup, reducing the possibility of exceptions.

3. Modify the class loader configuration of Tomcat

Sometimes, we can tweak Tomcat's class loader settings to ignore certain JAR packages or class files. This can be done by modifying the Tomcat or file.

In the file, you can ignore specific JAR packages by setting the jarsToSkip property of the Context tag:

<Context>
    <Parameter name="" value="*.jar" />
</Context>

After this configuration, Tomcat will not scan all JAR packages for annotation processing, thus avoiding similarException caused by the file.

4. Disable Tomcat's annotation scanning

If your project does not rely on annotation processing function, you can also choose to disable Tomcat's annotation scanning function. This can be done byAdd the following configuration to the file to implement it:

<context-param>
    <param-name></param-name>
    <param-value>*.jar</param-value>
</context-param>

This configuration tells Tomcat to skip annotation scans in all JAR packages to avoid startup failures due to parsing incompatible bytecode. This method is especially suitable for old projects that do not require annotation functions.

5. Confirm the correctness of dependency management

The last common cause of such problems is improper dependency management, resulting in different versions of dependency conflicts. Therefore, it is very important to check the dependency tree to ensure that there are no different versions of the same JAR package introduced.

Check for dependency conflicts using Maven

You can check the dependency tree of a Maven project using the following command:

mvn dependency:tree

By analyzing the dependency tree, confirm that there are no duplicate or conflicting dependencies, especially those JAR packages compiled from different versions of Java. If a conflict is found, it can be resolved by excluding redundant dependencies or locking the version.

Summarize

Invalid byte tag in constant pool: 19 error encountered when Tomcat starts, usually caused by incompatible Java versions and module systems. To solve this problem, we can avoid startup failures by upgrading Tomcat or JDK, excluding unnecessary JAR packages, modifying class loader settings, or disabling annotation scans.

Dependency management in projects is critical in Java development, especially when using multiple third-party libraries and frameworks, ensuring their version compatibility can avoid many difficult problems. Through the introduction of this article, I hope that readers can better understand the compatibility issues of Tomcat with Java versions and can quickly locate and solve similar issues in practice.

The above is the detailed content of the solution to the Invalid byte tag in constant pool exception when Tomcat starts. For more information about the error when Tomcat starts, please pay attention to my other related articles!