由于Fix project setup引起的项目异常

ide工具使用失误引起的工程问题

1.起由

两个maven项目,一个util工程,另一个zuul工程需要依赖util工程,zuul工程需要使用util工程的某些方法,所以需要引用util工程到zuul工程,但由于在编写的时候忘记导入util依赖,报错时使用了eclipse提示的fix project setup…,
在这里插入图片描述

2.问题

在fix后虽然编写报错没了,但在运行完全正确的源代码时,却出现异常,以下是最后打印的异常描述.

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

   org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1321)

The following method did not exist:

   javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

The method's class, javax.servlet.ServletContext, is available from the following locations:

   jar:file:/E:/eclipse/maven/Maven_Repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
   jar:file:/E:/eclipse/maven/Maven_Repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.37/tomcat-embed-core-9.0.37.jar!/javax/servlet/ServletContext.class

The class hierarchy was loaded from the following locations:

   javax.servlet.ServletContext: file:/E:/eclipse/maven/Maven_Repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext

3.分析解决

两个项目的依赖如下
util项目:

<dependencies>
   <!-- 引入 Servlet 容器中相关依赖 -->
   <dependency>
   	<groupId>javax.servlet</groupId>
   	<artifactId>servlet-api</artifactId>
   	<version>2.5</version>
   	<scope>provided</scope>
   </dependency>
</dependencies>

zuul项目:

<dependencies>
   <dependency>
   	<groupId>org.springframework.cloud</groupId>
   	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
   <dependency>
   	<groupId>org.springframework.cloud</groupId>
   	<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
   </dependency>
   <!-- 引入springboot&redis整合场景 -->
   <dependency>
   	<groupId>org.springframework.boot</groupId>
   	<artifactId>spring-boot-starter-data-redis</artifactId>
   </dependency>
   <!-- 引入springboot&springsession整合场景 -->
   <dependency>
   	<groupId>org.springframework.session</groupId>
   	<artifactId>spring-session-data-redis</artifactId>
   </dependency>
</dependencies>

看了下异常提示,就是有需要使用的类,有两个位置有,不知道用哪个,所以应该是依赖有问题,但是网上试了后并没有发现有什么问题

思路一:

想着可不可以把工程删除,然后把和工程代码无关的eclipse生成的东西删除,然后再maven导入能不能解决问题

删除eclipse的工程(可以先使用eclipse找到工程目录)
1.找工程目录,工程右键单击工程->properties
在这里插入图片描述
进入工程目录,然后回过来删除工程

2.删除eclipse的工程
在这里插入图片描述
3.删除无关代码的文件
在这里插入图片描述
4.到eclipse导入maven工程
file->import->Existing Maven Projects
找到工程路径并引入,然后他会自动导入项目
在这里插入图片描述
5.然后再重新把util依赖导入pom.xml(不要再fix projets)
这样就成功解决了问题

思路二:

后来才想起使用了fix project,然后看工程的configure build path,发现里面的order and export多了一个util项目,就查看了网上这个的作用,
eclipse的 build path 里面 的 order and export 选项卡的作用
发现可能是这个有问题,maven里面已经有了tomcat的依赖,里面就包含了servlet-api,而我的util项目由引入了servlet-api,可能是这个fix project并不能好好的处理多个依赖之间的关系,所以在configure build path的project标签删除后重新导入依赖依然没有问题
1.删除projects的util在这里插入图片描述
2.然后在pom.xml引入util依赖,
成功解决问题成功

4.总结

以后再遇到类似不是由于源代码的问题引起的异常,那就可以优先使用
删除工程->清理无关文件->再重新导入的方法解决
这样的思路具有更大的普遍性,能更快的解决问题,而思路二比较有争对性,往往需要更长的时间才能解决问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值