Maven java web 项目 单元测试 报 class not found 错误

1.class not found 说明在 Eclipse 去运行这个类的时候没有在指定的位置找到 class 文件,所以就会报 class not found 错误。

2.对于 Maven 项目来说,它 加载 class 的路径就是 projectName/target/classes 路径,而通常我们在开发 Maven 项目的时候,会修改 projectName/src/main/java 和 projectName/src/main/resources 的输出路径到:projectName/src/main/webapp/WEB-INF/classes 下面,这个时候如果我们是直接修改的如下的位置:


那么就相当于是把上面三个目录的输出路径都修改了,这就是问题所在,因为 projectName/src/test/java 目录下面都是 用来测试的类,这些类只能通过 Java Application 方式来运行,而 Maven 在运行 java 类的时候默认是从 adtarget-adaptor/target/classes 目录下进行加载的,很明显它的位置下面是没有class 文件的,因为输出路径已经被修改了。因此要解决这个问题,我们需要注意:只修改 第一个和第二个目录的 Output folder 属性就好了,如下图所示:


修改的方法:双击对应目录的 Output folder 属性,然后修改路径就可以了。

而第三个目录:adtarget-adaptor/src/test/java 的 Output folder 不需要修改。

3.这个时候项目就搞定了,你就可以进行 单元测试了,再也不会发生 class not found 的错误了。

The "javax.servlet.http.HttpServletRequest" class is part of the Java Servlet API, which is used for handling HTTP requests and responses in Java web applications. To resolve the issue of "HttpServletRequest not found", you need to make sure that you have included the necessary libraries in your project. Here are a few steps you can follow to resolve this issue: 1. Check your project dependencies: Make sure you have added the servlet API dependency in your project. If you are using a build tool like Maven, add the following dependency to your pom.xml file: ```xml <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> ``` 2. Verify your Servlet container: Ensure that you are using a Servlet container that supports the Servlet API. Common examples include Apache Tomcat, Jetty, or GlassFish. Make sure the container is properly configured and running. 3. Clean and rebuild your project: Sometimes, IDEs may not properly recognize or import classes. Try cleaning and rebuilding your project to ensure that all dependencies are correctly resolved. 4. Import the required package: If you are using an IDE like Eclipse or IntelliJ, make sure to import the "HttpServletRequest" class by adding the following import statement at the top of your Java file: ```java import javax.servlet.http.HttpServletRequest; ``` By following these steps, you should be able to resolve the "HttpServletRequest not found" issue. If you continue to encounter any problems, please provide more details about your project setup and any error messages you are receiving.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值