【问题标题】:VSCode Java extension gives error "Cannot find the class file for javax.servlet.http.HttpServletResponse."VSCode Java 扩展给出错误“找不到 javax.servlet.http.HttpServletResponse 的类文件。”
【发布时间】:2021-11-13 17:06:35
【问题描述】:

我有一个使用 gradle 作为构建系统的 java 项目。我正在使用 Redhat 的 Java 扩展在 VSCode 中进行开发。我正在使用 openjdk 11 在 Ubuntu 20.04 上开发。

当我使用“./gradlew assemble”从命令行构建项目时,我没有收到任何构建错误。

但是当我在vscode中打开项目时,在问题视图中出现以下问题...

The project was not built since its build path is incomplete. Cannot find the class file for javax.servlet.http.HttpServletResponse. Fix the build path then try building this project

还有……

The type javax.servlet.http.HttpServletResponse cannot be resolved. It is indirectly referenced from required .class files

我的build.gradle如下....

plugins {
  id 'com.google.cloud.tools.jib' version '2.4.0'
}

apply plugin: 'java'

// Use maven repository
repositories {
    mavenCentral()
    google()
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'org.eclipse.jetty:jetty-servlet:9.4.19.v20190610'
    implementation 'org.eclipse.jetty:jetty-client:9.4.19.v20190610'
    implementation group: 'org.eclipse.jetty', name: 'jetty-util', version: '9.4.29.v20200521'

    implementation 'mysql:mysql-connector-java:8.0.16'
    implementation 'com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.0.16'
    implementation 'org.apache.commons:commons-dbcp2:2.7.0'
    implementation 'org.jdbi:jdbi3-core:3.10.1'
    implementation 'com.google.firebase:firebase-admin:6.11.0'
    implementation 'com.google.apis:google-api-services-oauth2:v1-rev155-1.25.0'

    implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
    implementation group: 'org.reflections', name: 'reflections', version: '0.9.12'

    implementation 'com.github.bhlangonijr:chesslib:1.2.3'

    // compile project('chesslib')
    //runtime files('../../chesslib/build/libs/chesslib-1.1.12.jar')
}

task runServer(type: JavaExec) {
    enableAssertions = true
    classpath = sourceSets.main.runtimeClasspath 
    main = 'ChessServer'
} 

task runClient(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath 
    main = 'ChessClient'
    standardInput = System.in // without this, using Scanner on System.in won't work
}

task runCLITestApp(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath 
    main = 'CLITestApp'
    standardInput = System.in
}

// use cloudBuilder.py to invoke this jib task to build the container
// use cloudConfiguration.json to configure things like container version.
jib {
    if (project.hasProperty('googleCloudProjectName') && project.hasProperty('imageName')) {
        to {
          image = "gcr.io/$googleCloudProjectName/$imageName"
        }
        container {
            mainClass = 'ChessServer'
            ports = ['8080']
            creationTime = "USE_CURRENT_TIMESTAMP"
        }
    }
}

// make sure the required project properties are set for jib
task jibCheck {
    doLast {
        if (!project.hasProperty('googleCloudProjectName'))
            throw new GradleException("When invoking the jib task, make sure to pass the cli arg -PgoogleCloudProject=[myGoogleCloudProject]\nThis should be the name of the google cloud project which container registry you want to push to.");
        if (!project.hasProperty('imageName'))
            throw new GradleException("When invoking the jib task, make sure to pass the cli arg -PimageName=[imageName]");
    }
}
tasks.jib.dependsOn tasks.jibCheck

// src/main/jib is the default folder that jib looks for files to add to your container
// these files will be placed in the root / dir of the container
task moveFilesForJib(type: Copy) {
    from "releaseFiles"
    into "src/main/jib"
}
tasks.jib.dependsOn tasks.moveFilesForJib

【问题讨论】:

    标签: java gradle visual-studio-code


    【解决方案1】:

    将以下内容添加到build.gradle。重建项目后,看看错误是否消失。

    compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    

    【讨论】:

    • 这并没有什么不同。我怀疑这个问题与 build.gradle 无关,与 vscode 扩展配置有关。
    • @Scorb。请注意,Java 扩展需要 jdk11 或最新版本来支持。打开Command Palette,选择Java:Clean Java Language Server Workspace,有帮助吗?
    • java.home 没有设置,到目前为止它工作得很好。我在 ubuntu 20.04 上使用 openjdk 11。 Java 默认添加到环境中。
    • @Scorb 您的机器上是否有多个 JDK?
    【解决方案2】:

    解决方案是运行

    ./gradlew wrapper
    

    然后在 vscode 中运行 Java: Clean Workspace。

    【讨论】:

    • 您可以接受您的回答以显示问题已解决。
    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 2012-08-23
    • 2015-07-11
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多