我有 java.lang.ClassNotFoundException 错误。很简单,但是我解决不了!
当我检查文件夹时
WebContent\WEB-INF\classes
我看到它是空的。这意味着无法构建类。
我有一个 Maven 项目,我做了几次:Maven clean,Maven install,但我仍然有同样的错误。
当我查看我的 Java 构建路径窗口时,在源选项卡中,我看到默认输出文件夹是:MyProject/WebContent/WEB-INF/classes。
同样在此窗口中,复选框:允许源文件夹的输出文件夹未选中,即使我选中它并应用并保存它,当我运行项目时,它会再次自动取消选中。奇怪的!似乎其他东西正在重写我的构建路径。
您还可以在我的图像中看到我的 Web 部署程序集似乎是正确的。
我清理 Tomcat 服务器并清理项目并重新构建它。但这没有帮助。
我的 web.xml 基于我的项目层次结构图像:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SimBio</display-name>
<welcome-file-list>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SimServlet</servlet-name>
<servlet-class>simbio.main.SimServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimServlet</servlet-name>
<url-pattern>/SimServlet</url-pattern>
</servlet-mapping>
</web-app>
我用的是Apache Tomcat 7.0,我的项目来了
HTTP Status 500 - Error instantiating servlet class simbio.main.SimServlet
exception
javax.servlet.ServletException: Error instantiating servlet class simbio.main.SimServlet
root cause
java.lang.ClassNotFoundException: simbio.main.SimServlet
我的项目有什么问题。我还应该检查什么?
非常感谢您的帮助。
更新: 在环境变量中我有这些:
JAVA_HOME: C:\Program Files\Java\jre1.8.0_191
JAVA_PATH: C:\Program Files\Java\jre1.8.0_191\bin
更新2 我的pom.xml(依赖于其他项目,如果运行那些Maven依赖项目出错,我的项目能生效吗?其实我的项目是旧的,这些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>SimBio</groupId>
<artifactId>SimBio</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-model</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.github.sharispe</groupId>
<artifactId>slib-graph</artifactId>
<version>0.9.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.github.sharispe</groupId>
<artifactId>slib-graph-model</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.github.sharispe</groupId>
<artifactId>slib-utils</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>com.github.sharispe</groupId>
<artifactId>slib-tools-sml-toolkit</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>com.github.sharispe</groupId>
<artifactId>slib-sml</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>com.github.sharispe</groupId>
<artifactId>slib-graph-algo</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
更新 3: 我认为我的问题与其他 depenedt 项目有关(目前无法安装他们的 maven。我的错误列表中有一个错误,即:构建路径不完整。
最佳答案
解决了!
我不知道到底是什么帮助了我,但我做了一些改变,总的来说它解决了我的问题:
- 无法构建类时,项目的pom.xml和其他相关项目的pom.xml有错误。
- 当Allow output folders for source folders的复选框不能hold时,说明另一个东西正在重写,所以JAVA_HOME一定要和项目设置一致
- 我还将输出文件夹添加为 MyProject/target/classes
- 我也检查了我的项目中JDK/JRE的版本和其他相关项目以及Tomcat是否相同。
- 我的 servlet 类的编译错误也可能导致:Error instantiating servlet class
关于java - WebContent\WEB-INF\classes 文件夹是空的。 Java 构建路径找不到我的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929279/