升级Ubuntu 16.04至18.04后Tomcat 8.5无法启动,报NoSuchMethodError错误
That NoSuchMethodError related to ByteBuffer.limit(I)Ljava/nio/ByteBuffer; is almost always caused by a Java version mismatch between what Tomcat expects and what it's actually running on. Here's how to resolve it:
Root Cause
Tomcat 8.5 requires a minimum of Java 8, but the error indicates your Tomcat instance is either running on an older Java version (pre-8) or there's a conflict between the Java version Tomcat was compiled for and the one in use. Ubuntu 18.04 ships with OpenJDK 11 by default, but after upgrading, multiple Java versions might be installed, and Tomcat could be picking the wrong one.
Step-by-Step Solutions
1. Verify the Java Version Tomcat is Using
First, check your system's default Java version:
java -version
For Ubuntu's packaged Tomcat, check the configured JAVA_HOME:
cat /etc/default/tomcat8.5 | grep JAVA_HOME
If you installed Tomcat manually, look for a setenv.sh file in the bin directory of your Tomcat installation to see which Java version it's using.
2. Set the Correct Java Version as Default
If multiple Java versions are installed, use update-alternatives to set Java 8 or 11 (both compatible with Tomcat 8.5) as the default:
sudo update-alternatives --config java sudo update-alternatives --config javac
Select the number corresponding to Java 8 or 11 from the provided list.
3. Explicitly Set JAVA_HOME for Manual Tomcat Installations
For manual Tomcat setups, create or edit the setenv.sh file in Tomcat's bin directory to point to the correct Java installation. For example:
sudo nano /opt/tomcat8.5/bin/setenv.sh
Add these lines (adjust the path to match your Java installation):
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH
Save the file and make it executable:
sudo chmod +x /opt/tomcat8.5/bin/setenv.sh
4. Update Ubuntu's Packaged Tomcat
If you're using the official Ubuntu Tomcat package, ensure it's updated to the latest version compatible with Ubuntu 18.04:
sudo apt update && sudo apt upgrade tomcat8.5
5. Check for Conflicting Libraries in Web Apps
Occasionally, web applications include outdated or incompatible JAR files in their WEB-INF/lib directory that conflict with Tomcat's dependencies. Scan your apps' WEB-INF/lib folders for any JARs containing Java core classes (like nio-related files) and remove them—these should never be included in a web app's lib directory.
After making these changes, restart Tomcat:
sudo systemctl restart tomcat8.5
内容的提问来源于stack exchange,提问作者shyam

