ClassNotFoundException: WriteException/HSSFAnchor

原创
2016/06/14 18:52
阅读数 195

 使用net.sourceforge.jexcelapi.jxl和org.apache.poi做个测试,pom.xml内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>hui</groupId>
	<artifactId>TestWithMaven</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>TestWithMaven</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.13</version>
		</dependency>
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-core</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.0</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>org.hamcrest</groupId>
			<artifactId>hamcrest-core</artifactId>
			<version>1.3</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.2.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>net.sourceforge.jexcelapi</groupId>
			<artifactId>jxl</artifactId>
			<version>2.6.12</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.14</version>
		</dependency>



	</dependencies>
</project>

1.测试jxl导出excel时,main方法测时正常,但是布署写成Servlet形式,布署到Tomcat 8上就报错:

ClassNotFoundException: jxl.write.WriteException.

同理:.测试poi导出excel时,main方法测时正常,但是布署写成Servlet形式,布署到Tomcat 8上就报错:

ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFAnchor

显然依赖里有的,解决方法是:

1).治标不治本方法(虽也能成功,但后期比如用jersey测试时,启动时会报ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

将依赖的jar文件jxl-2.6.12.jar、poi-3.14.jar 拷贝到Tomcat的安装目录下的lib目录里,如:D:\apache-tomcat-8.0.35\lib,然后重启即可。

2).  治本方法:

导致这一原因的方法,是maven依赖会作为entry,即未自动加入到WEB-INF/lib目录下,这时我们需要:

右键项目-->Properties-->Deployment Assembly,如果展开  的页面中没有Maven dependencies,这时我们点击右上角的Add-->Java Build Path Entries-->Maven Dependencies-->finish。

这时maven依赖即自动加入到WEB-INF/lib目录下,虽然你看不到。然后重启服务器,即正常。

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部