当我使用以下方法执行 jar 时遇到以下错误java -jar .\QuartzScheduler-0.0.1-SNAPSHOT.jar
Error: Could not find or load main class com.quartz.QuartzSchedulerApplication
Caused by: java.lang.ClassNotFoundException: com.quartz.QuartzSchedulerApplication
这是我的 build.gradle 的一个片段,请帮助阐明这一点。
构建.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 的实现。