【问题标题】:WebContent\WEB-INF\classes folder is empty. Java build path cannot find my classesWebContent\WEB-INF\classes 文件夹为空。 Java 构建路径找不到我的类
【发布时间】:2019-11-17 15:15:31
【问题描述】:

我有 java.lang.ClassNotFoundException 错误。很简单,但我解决不了!
当我检查文件夹时

WebContent\WEB-INF\类

我看到它是空的。这意味着无法构建类。
我有一个 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 状态 500 - 实例化 servlet 类时出错 simbio.main.SimServlet


异常
javax.servlet.ServletException: 实例化错误 servlet 类 simbio.main.SimServlet
根本原因
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>

Update3:我认为我的问题与其他依赖项目有关(他们的 maven 目前无法安装。我的错误列表中有一个错误,即:构建路径不完整。

【问题讨论】:

  • 您也可以发布您的 pom.xml。您的项目结构似乎与 Maven 约定不一致(例如,源文件应位于 src/main/java 和 src/main/webapp 下。请参阅本指南 - maven.apache.org/guides/mini/…
  • 我添加了我的 pom.xml。我的项目使用另一个现有项目(它取决于另一个)。我现在检查了,在那些项目中, pom.xml 无法运行。他们有一个错误。我正在尝试解决它们并运行那些 pom.xml 文件,但到目前为止,还没有结果。
  • 根据我输入的pom.xml,我还更新了部署程序集的镜像。
  • 是的,我也已经尝试过使用 MyProject/target/classes,但是当我运行项目时,该复选框没有保持选中状态。而且在 target\classes 中,也没有文件
  • 你可以尝试在你的 配置中使用它吗?WebContent/WEB-INF/classes

标签: java maven tomcat


【解决方案1】:

解决了!
我不知道什么对我有帮助,但我做了一些改变,总的来说它解决了我的问题:

  • 无法建类时,项目的pom.xml和其他相关项目的pom.xml有错误。
  • 当源文件夹的允许输出文件夹的复选框不能保持时,表示另一件事正在重写,所以请确保JAVA_HOME与项目设置相同
  • 我还将输出文件夹添加为 MyProject/target/classes
  • 我还检查了我的项目和其他相关项目中的 JDK/JRE 的版本,并且 Tomcat 也是一样的。
  • 我的 servlet 类上的编译错误也可能导致:错误实例化 servlet 类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-20
    • 2012-01-11
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多