我有一个使用gradle的spring boot项目
apply plugin: 'java' apply plugin: 'idea' apply plugin: 'jetty' apply plugin: 'war' apply plugin: 'org.springframework.boot' repositories { mavenCentral() } dependencies { compile("org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE") { exclude module: "spring-boot-starter-tomcat" } compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty', version: '1.5.1.RELEASE' compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.5.1.RELEASE' compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.6.RELEASE' compile group: 'org.springframework', name: 'spring-context-support', version: '4.3.6.RELEASE' compile group: 'org.springframework', name: 'spring-orm', version: '4.3.6.RELEASE' compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' compile group: 'org.freemarker', name: 'freemarker', version: '2.3.23' compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5' compile group: 'org.hibernate', name: 'hibernate-core', version: '5.1.4.Final' compile group: 'commons-validator', name: 'commons-validator', version: '1.5.1' compile group: 'junit', name: 'junit', version: '4.12' compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.1.RELEASE' } task wrapper(type: Wrapper) { gradleVersion = '2.2' } buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE") } } jar { baseName = 'base-name' version = '0.1.0' manifest { attributes 'Main-Class': 'some.application.Application' } } sourceCompatibility = 1.8 targetCompatibility = 1.8
我使用构建, ./gradlew clean jar 然后尝试运行 java -jar <jar-name>.jar
./gradlew clean jar
java -jar <jar-name>.jar
但是我收到一个错误消息
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication at some.application.Application.main(Application.java:11) Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
我对为什么可以在IDE中而不是作为jar运行它感到困惑。另外我发现我的jar根本不包含任何库。
编辑:
这是我的 Application.class
Application.class
打包一些应用程序;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
这是因为您的依赖项未包含在最终jar文件中。
在这里或这里看看示例。
或者,使用./gradlew clean build可以自动正确打包应用程序。看看官方指南
./gradlew clean build