java - 找不到主要/资源中的类

标签 java gradle spring-boot

我正在使用Spring Boot gradle插件来构建可执行文件。我在src / main / resources中有一个FindResource.java类来查找文件:

FindResource.class.getResource(templateName).toURI()

当我执行gradle build时,我得到一个错误,该类FindResource无法解析。我需要Spring Boot gradle插件吗,它还应该使用resources目录中的类。我该怎么办?

我的build.gradle如下所示:
buildscript {
    ext {
        springBootVersion = '1.2.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
        classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot' 
apply plugin: 'io.spring.dependency-management' 

jar {
    baseName = 'abc'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-jersey")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.apache.pdfbox:pdfbox:1.8.10")
    compile('org.apache.poi:poi-ooxml:3.12')
    compile('org.apache.poi:poi-scratchpad:3.12')
    runtime("org.hsqldb:hsqldb")
    testCompile("org.springframework.boot:spring-boot-starter-test") 
}


task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

最佳答案

如注释类中所述,要加载的文件必须位于src / main / java /中,而不是src / main / resources中。 This link可以帮助您提供有关此结构约定的更多信息。

关于java - 找不到主要/资源中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31959764/

相关文章:

Java 应用程序不显示输出

java - 在 while 循环中将文件同步到服务器

java - 无法从 Elastic 6.0 获取数据 : “error” :”Content-Type header [text/plain] is not supported”, ”status”:406

android - 文件或目录 '<project>/<app>/src/debug/java' ,未找到

java - mvnw 和 mvnw.cmd 文件的用途是什么?

java - Spring Boot 验证该字段是字符串类型

java - 从列表中删除不相关的项目与使用相关项目构建新列表

java - 导入build.gradle时出现Intellij错误

android - Gradle的依赖项缓存似乎已损坏或不同步

java - 使用 init.d 运行 SpringBoot Jar 时出错