记一次java.lang.NoSuchMethodError(Jar包冲突)问题解决的方法

本文探讨了在使用Apache POI时遇到的jar包冲突问题,通过类名定位和classpath检查,作者提供了识别和解决方法调用错误的步骤,最终建议调整jar包依赖以避免此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近使用Poi有关Jar包时,发现运行程序时控制台报错:

Exception in thread "main" java.lang.NoSuchMethodError: org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth.setW(Ljava/lang/Object;)V
	at org.apache.poi.xwpf.usermodel.XWPFTable.createEmptyTable(XWPFTable.java:193)
	at org.apache.poi.xwpf.usermodel.XWPFTable.<init>(XWPFTable.java:165)
	at org.apache.poi.xwpf.usermodel.XWPFTable.<init>(XWPFTable.java:147)
	at org.apache.poi.xwpf.usermodel.XWPFDocument.createTable(XWPFDocument.java:994)
	at org.apache.poi.examples.xwpf.usermodel.SimpleTable.createSimpleTable(SimpleTable.java:73)
	at org.apache.poi.examples.xwpf.usermodel.SimpleTable.main(SimpleTable.java:56)

通过Exception定位有关路径后,发现存在此方法,但是参数是(java.math.BigInteger w)不是Exception中的(java/lang/Object),所以就报了无此方法的错。

此时应该意识到可能是jar包冲突,就是不同的Jar包都有一个同样的路径,一般是版本冲突,也有可能不是,就是A.jar和B.jar中都有org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth这个类,JVM加载类时没有加载到需要调用的正确的类。

想来想去,还是写了两个方法(比较粗糙)来快速识别一下Jar包冲突的问题,便于快速定位问题。

思路是:先根据类名查询JVM加载的是哪个Jar包。然后获取到classpath,然后将classpath中的jar包遍历,寻找指定的类名,将符合jar包冲突的信息打印到屏幕,然后人工核实。使用时需要把代码加到所在项目中去运行。

import java.io.IOException;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class GetClass {

	public static void main(String[] args) {
		String PakageString = "org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth";
		GetFilePathByPackage(PakageString);
		System.out.println("\r\n");
		// 获取Class路径
		String alljar = System.getProperty("java.class.path");
		String[] alljarpath = alljar.split(";");
		// 根据路径遍历,该路径下的Jar包是否有该包,有就打印
		for (String jarpath : alljarpath) {
			if (jarpath.contains("jar")) {
				String[] jar = new String[2];
				jar[0] = jarpath;
				jar[1] = PakageString;
				getClassNameByJarAndPackage(jar);
			}
		}
	}

	/**
	 * 根据类名打印JVM加载的类对应的Jar包路径,如GetFilePathByPackage("java.util")。
	 * 
	 * @param PakageString 类名
	 */
	public static void GetFilePathByPackage(String PakageString) {
		System.out.println("查找的类:" + PakageString);

		try {
			Class<?> c = Class.forName(PakageString);
			ProtectionDomain pd = c.getProtectionDomain();
			CodeSource cs = pd.getCodeSource();
			System.out.println("加载的类对应的路径:" + cs.getLocation().toString());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 根据jar包路径和包名来查询jar包中是否含有有关类
	 * 
	 * @param jar jar[0] jar路径 jar[1] 包名
	 */
	@SuppressWarnings("resource")
	public static void getClassNameByJarAndPackage(String[] jar) {
		String jarFilePath = jar[0];
		String packagePath = jar[1].replace(".", "/");
		try {
			JarFile jarFile = new JarFile(jarFilePath);
			Enumeration<JarEntry> entrys = jarFile.entries();
			while (entrys.hasMoreElements()) {
				JarEntry jarEntry = entrys.nextElement();
				String entryName = jarEntry.getName();
				if (entryName.equals(packagePath + ".class")) {
					System.out.println("类路径:" + entryName + "\r\n" + "jar包的路径:" + jarFilePath);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

执行结果:

由此发现:

JVM加载了ooxml-schemas-1.4.jar,而lib下poi-ooxml-full-5.0.0.jar和poi-ooxml-lite-5.0.0.jar也有相同路径。

经过核实,poi-ooxml的两个jar包都有setW(Object)这个方法。所以只需要保留了一个jar就可以了(还是对Poi不熟悉导致了Jar包冲突啊),去掉另外两个Jar包的引用,保留poi-ooxml-full-5.0.0.jar。就可以避免出现方法不存在的问题了。如果确实以上Jar包都需要引用,可以在eclipse中调整Jar包的加载顺序。对于自己开发的Jar包还是建议改一下Jar包的结构。

要获取列的字符串格式,您需要使用Apache POI库中的`XWPFTableCell`类和OpenXML的CTTc(列定义)对象。以下是一个示例代码,演示如何获取列的字符串格式: ```java import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.FileInputStream; import java.io.IOException; public class WordTableFormatReader { public static void main(String[] args) { try { // 读取Word文档 FileInputStream fileInputStream = new FileInputStream("input.docx"); XWPFDocument document = new XWPFDocument(fileInputStream); fileInputStream.close(); // 获取第一个表格 XWPFTable table = document.getTables().get(0); // 假设文档中只有一个表格 // 获取表格列数 int columnCount = table.getRow(0).getTableCells().size(); // 遍历表格列的格式 for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { // 获取第一行的单元格 XWPFTableCell cell = table.getRow(0).getCell(columnIndex); // 获取CTTc对象 CTTc ctTc = cell.getCTTc(); // 获取列的字符串格式 if (ctTc.isSetTcPr()) { CTTcPr tcPr = ctTc.getTcPr(); if (tcPr.isSetTcW()) { CTTcPrBase.TcW tcW = tcPr.getTcW(); System.out.println("列 " + (columnIndex + 1) + " 的宽度类型: " + tcW.getType()); System.out.println("列 " + (columnIndex + 1) + " 的宽度值: " + tcW.getW()); } } } System.out.println("表格列格式读取成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们首先使用`FileInputStream`来读取名为"input.docx"的Word文档,然后使用`XWPFDocument`类加载文档内容。 接下来,我们使用`document.getTables().get(0)`获取文档中的第一个表格。如果您的文档中有多个表格,请根据实际情况选择要读取格式的表格。 然后,我们使用`table.getRow(0).getTableCells().size()`获取表格的列数。这里假设第一行中的单元格数与其他行相同。 接下来,我们使用一个循环来遍历每一列,并获取每个单元格的CTTc对象。然后,我们检查CTTc对象是否设置了TcPr(列属性)。如果设置了TcPr,我们进一步获取TcW(列宽)对象,并打印出宽度类型和宽度值。 请确保在代码中引入了正确的POI库以及其他所需的依赖项,并且在代码中导入了正确的类和包。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我一时想不起

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值