(Springboot Gradle可执行Jar)无法找到或加载主类

问题描述 投票:0回答:1

当我使用

java -jar .\QuartzScheduler-0.0.1-SNAPSHOT.jar

执行 jar 时遇到以下错误
Error: Could not find or load main class com.quartz.QuartzSchedulerApplication
Caused by: java.lang.ClassNotFoundException: com.quartz.QuartzSchedulerApplication

这是我的 build.gradle 的一个片段,请帮助阐明这一点。

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.1'
    id 'io.spring.dependency-management' version '1.1.5'
}

group = 'com.quartz'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-quartz'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc:2.4.1'
    implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0'
    implementation files('C:\\Program Files\\Microsoft JDBC DRIVER 12.6 for SQL Server\\sqljdbc_12.6\\enu\\jars\\mssql-jdbc-12.6.3.jre11.jar')
    runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.withType(Jar) {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    manifest {
        attributes["Main-Class"] = "com.quartz.QuartzSchedulerApplication"
    }
}

jar {
    enabled = true
    archiveClassifier = ''

    manifest {
        attributes 'Main-Class' : "com.quartz.QuartzSchedulerApplication"
    }

    from {
        configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

QuartzSchedulerApplication.class

package com.quartz.QuartzScheduler;

import com.quartz.QuartzScheduler.model.User;
import com.quartz.QuartzScheduler.repo.UserRepo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@SpringBootApplication
public class QuartzSchedulerApplication {

    public static void main(String[] args) {

        try {
            ApplicationContext context = SpringApplication.run(QuartzSchedulerApplication.class, args);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

任何帮助将不胜感激。提前致谢!

我开始使用 Log4j 构建我的 Quartz 项目,并且取得了成功。 另外,我验证了 jar 文件包含我的主类,并且清单文件也存在。当我使用 bootJar 运行应用程序时,它也成功了。 我还尝试使用 java -cp .\QuartzScheduler-0.0.1-SNAPSHOT.jar com.quartz.QuartzSchedulerApplication 执行 jar 文件。 不确定是否是由于 JDBC 的实现所致。

spring-boot jdbc build.gradle quartz-scheduler executable-jar
1个回答
0
投票

问题出在你的构建文件上。您正在处理 Gradle Spring Boot 插件所做的工作。接下来,您混合来自不同 Spring Boot 版本的模块也永远不会这样做。

解决方案非常简单,简化/清理你的

build.gradle

  1. spring-boot-starter-jdbc
  2. 中删除版本
  3. 删除
    jar
    任务和
    jar
    任务配置。
  4. 删除重复的
    mssql-jdbc
    依赖项(删除文件一个)
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.1'
    id 'io.spring.dependency-management' version '1.1.5'
}

group = 'com.quartz'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-quartz'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0'
    runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}

现在只需像平常一样使用

./gradlew build
构建你的 jar,Spring Boot 会自动为你创建一个 jar,你可以使用
java -jar QuartzScheduler-0.0.1-SNAPSHOT.jar
运行。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.