13

I have a spring boot project using gradle

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

I build using
./gradlew clean jar
Then try to run using
java -jar <jar-name>.jar

However I get an error saying

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

I am so confused as to why I can run it in IDE but not as a jar. Also I found out that my jar doesn't contain any libraries at all.

EDIT:

This is my Application.class

package some.application;

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);
    }
}
8
  • I would compare the paths that IDE is using and the contents of the jar Commented Feb 25, 2017 at 16:09
  • do you have the springboot entry class in your code ? Commented Feb 25, 2017 at 16:17
  • @EduardoDennis edited question showing my Application.java. Is that what you meant by spring boot entry class? Commented Feb 25, 2017 at 16:21
  • @efekctive I just checked my jar, and apparently it does not contain any spring boot jars at all whatsoever. This explains then why it can't run anything. Do you know how I shove spring boot jars into my jar? Commented Feb 25, 2017 at 16:21
  • what happens when you do gradle install ? It doesnt bring in the dependencies? Commented Feb 25, 2017 at 16:24

4 Answers 4

13

This is because your dependencies are not included into the end jar file.

Take a look on the examples here or here.

Or alternatively use ./gradlew clean build that should automatically pack the app correctly. Take a look on the official guide

Sign up to request clarification or add additional context in comments.

1 Comment

beautiful. Adding from { (configurations.runtime).collect { it.isDirectory() ? it : zipTree(it) } } worked like a charm for me :) thank you!
1
  1. Define a task called fatJar in build.gradle:
jar {
    baseName = 'base-name'
    version = '0.1.0'
    manifest {
        attributes(
                'Main-Class': 'com.github:'
        )
    }
}

task fatJar(type: Jar) {
    manifest.from jar.manifest
//    classifier = 'all'
    from {
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}

artifacts {
    archives fatJar
}
  1. Run gradle fatJar or ./gradlew fatJar to generate a jar file with all dependencies, which can run independently but may be pretty large in size (that's why it's called a fat jar).

Comments

1

From the official Spring-Boot-Site - link

Try creating a jar with: gradle clean bootJar

1 Comment

Hello! Thank you for your answer. Can you please provide more information on how this helps OP's problem? Answers should preferably be self contained, meaning all necessary information to understand the answer should be written in the answer and not in external links because links can break over time. (Feel free to quote a source and link to it, but please put the important information here)
-1

I tried out Sasha Shpota answer and that was my problem too... finally after hours of searching that helped me thanks a lot <3 added this to my grade.build:

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

Better look at Sasha Shpotas answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.