java.lang.NoClassDefFoundError: when trying to run jar - java

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);
}
}

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

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
}
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).

Related

NoClassDefFoundError with gradle multi projects

I have setup a gradle multi project in java. The build was successful but when I tried to run the JAR, a java.lang.NoClassDefFoundError was thrown.
Here is my project structure:
.gradle/
authserver/
build/
src/main/java/fr/evywell/robserver/auth/
Main.java
build/
common/
build/
src/main/java/fr/evywell/robserver/common/
gradle/
build.gradle
settings.gradle
And then my configuration:
settings.gradle (IN ROOT DIR)
rootProject.name = "robserver"
include 'authserver'
include 'common'
build.gradle (IN ROOT DIR)
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
build.gradle (IN common DIR)
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
}
build.gradle (IN authserver DIR)
apply plugin: 'application'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
compile project (':common')
}
mainClassName = 'fr.evywell.robserver.auth.Main'
jar {
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}
Error message:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: fr/evywell/robserver/common/network/Server
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: fr.evywell.robserver.common.network.Server
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
The error message says that it cant find a class in my common package.
I don't know why because I used compile project (':common') in my authserver gradle configuration
Here is the command: java -jar authserver/build/libs/authserver.jar
Thank you for your help and have a nice day !
For me worked next thing: in kotlin I haved spring boot plugin applied to all subprojects {} in root build.gradle
Instead of this I've removed
apply(plugin = "org.springframework.boot") from root build.gradle and applied this line for each subproject instead of common module
I think the issue is that the JAR you are creating does not include the class files generated from compiling the common module. Try ammending the jar task of authserver module so that you include the output of compiling the common module:
jar {
from project.sourceSets.main.allSource
from project(":common").sourceSets.main.java.output
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}
I used the shadow plugin to build a "fat jar". I don't know if it's a good practice...
Thank you for your help !
// authserver/build.gradle
plugins {
id 'com.github.johnrengelman.shadow' version '5.1.0'
id 'java'
}
apply plugin: 'application'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
compile project (':common')
}
mainClassName = 'fr.evywell.robserver.auth.Main'
jar {
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}

Java 8 intellij and gradle fat jar wont find configuration files and enableautoconfiguration

I am trying to build fat jar, but the jar is giving me error. I have built fat jar before with javaFx 11 and java 11. Now I am using Java 8 and Spring I am doing it with intellij from File>Project Structure>Artifacts, there I choose ProjectRoot main module and then also add my resources directory content and I build the jar.
[main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.boo t.enableautoconfiguration' in any property source
21:31:42.635
[main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
at org.springframework.util.Assert.notEmpty(Assert.java:450)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:160)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:96)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.process(AutoConfigurationImportSelector.java:386)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:828)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:563)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at com.tick42.QuicksilverApplication.main(QuicksilverApplication.java:13)
My project is:
>ProjectRoot
>src
>main
>resources
>java
>com
>projectId
>restaurant
RestaurantApplication.class
In restaurant folder are all my config and project classes.
My gradle build is at the project root:
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
group = 'com.projectId'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-security")
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.2.RELEASE'
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.6.0'
compile group: 'com.mchange', name: 'c3p0', version: '0.9.5.2'
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.0.8.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '5.3.5.Final'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
compile group: 'com.googlecode.log4jdbc', name: 'log4jdbc', version: '1.2'
compile "org.springframework.boot:spring-boot-configuration-processor"
compile 'org.passay:passay:1.3.1'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.kohsuke', name: 'github-api', version: '1.93'
}
jacocoTestReport {
reports {
xml.enabled = true
html.enabled = true
}
}
check.dependsOn jacocoTestReport
settings.gradle:
rootProject.name = 'server-side'
My RestaurantAplication.class is was in my restaurant folder, but I read somewhere that it should be above my package with my classes, so I moved it to the projectId folder, but it didn't work.
My main:
#SpringBootApplication
#EnableScheduling
#EnableConfigurationProperties
public class RestaurantApplication {
public static void main(String[] args) {
SpringApplication.run(RestaurantApplication.class, args);
}
}
Edit: in the jars's spring.factories in the META-INF folder there is only one line:
org.springframework.beans.BeanInfoFactory=org.springframework.beans.ExtendedBeanInfoFactory
I tried adding them to the file also adding some of the ones listed in the error and also my config files:
org.springframework.beans.BeanInfoFactory=org.springframework.beans.ExtendedBeanInfoFactory
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector
org.springframework.context.annotation.ConfigurationClassParser
com.projectId.restaurant.config.AppConfig
com.projectId.restaurant.config.PasswordEncoderConfig
com.projectId.restaurant.config.ScheduleConfig
com.projectId.restaurant.config.Scheduler
com.projectId.restaurant.config.SecurityConfig
But even then the lines in the error still exist for the exact configurations I have added:
...
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:160)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:96)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.process(AutoConfigurationImportSelector.java:386)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:828)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:563)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)
...

Idea debug or run

When I try to run the gradle project from Idea I've an error
FAILURE: Build failed with an exception.
What went wrong: Task 'run Application' not found in root project 'app-search'.
Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. CREATE IMAGE BUILD FAILED
Total time: 0.153 secs Task 'run SearchApplication' not found in root
project 'app-search'.
Please how to solve it.?
This is the main part of my build.gradle
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'docker'
apply plugin: 'com.github.johnrengelman.shadow'
sourceCompatibility = 1.8
ext {
dropwizardVersion = '1.1.2'
mainClass = "com.jobservice.search.AppLauncher"
mapstructVersion = '1.1.0.Final'
}
mainClassName = mainClass
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
buildscript {
repositories { jcenter() }
dependencies {
classpath 'se.transmode.gradle:gradle-docker:1.2'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
}
}
// add logging into test stream
test {
testLogging.showStandardStreams = true
}
dependencies {
compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.1'
compile group: 'org.slf4j', name: 'slf4j-ext', version: '1.7.25'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
compile 'com.google.inject:guice:3.0'
compile group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version: '1.5.15'
compile group: 'io.swagger', name: 'swagger-annotations', version: '1.5.15'
compile group: 'com.hubspot.dropwizard', name: 'dropwizard-guice', version: '1.0.6.0'
compile group: 'org.liquibase', name: 'liquibase-core', version: '3.5.3'
compile (
'io.dropwizard:dropwizard-core:' + dropwizardVersion,
'io.dropwizard:dropwizard-hibernate:' + dropwizardVersion,
'org.postgresql:postgresql:9.4.1208.jre7',
'io.dropwizard:dropwizard-testing:' + dropwizardVersion
)
compile 'org.mapstruct:mapstruct-jdk8:' + mapstructVersion
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.hamcrest', name: 'hamcrest-junit', version: '2.0.0.0'
testCompile 'org.mockito:mockito-core:2.0.54-beta'
testCompile group: 'com.h2database', name: 'h2', version: '1.4.194'
testCompile('org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:2.23.2') {
exclude group: 'javax.servlet', module: 'javax.servlet-api'
exclude group: 'junit', module: 'junit'
}
}
// Configure the shadow jar task
shadowJar {
classifier = 'dist'
baseName = 'jobs-search'
version = "1.0"
mergeServiceFiles()
manifest {
attributes 'Main-Class': mainClassName
}
version = ''
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}

ClassNotFoundException when running main class in Intellij gradle project

When I try to run main method in my gradle project using run configuration from IDEA Intellij option I'm getting exception as follows:
15:12:03: Executing external task 'run'...
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder
at pl.edu.agh.flowshop.utils.ConfigReader.getMachinesConfig(ConfigReader.java:46)
at pl.edu.agh.flowshop.utils.ConfigReader.createModel(ConfigReader.java:30)
at pl.edu.agh.flowshop.Experiment.main(Experiment.java:25)
Caused by: java.lang.ClassNotFoundException: com.google.gson.GsonBuilder
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)
... 3 more
It bothers me because if I just create runnable jar from same configuration everything works fine. I've tried refrehing gradle project as well as 'Invalidate Caches/Restart' option but none of them helped.
My build.gradle file:
group 'pl.edu.agh'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = ['src/main/java']
mainClassName = 'pl.edu.agh.flowshop.Experiment'
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'nz.ac.waikato.cms.weka', name: 'weka-stable', version: '3.6.6'
compile group: 'commons-io', name: 'commons-io', version: '2.4'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
compile name:'piqle'
}
jar {
manifest {
attributes(
'Implementation-Title': project.name,
'Implementation-Version': version,
'Built-By': System.getProperty('user.name'),
'Built-Date': new Date(),
'Built-JDK': System.getProperty('java.version'),
"Main-Class": mainClassName
)
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}

Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter

I am using spring Boot.I created Spring boot gradle application.I got following error.
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter
build.gradle
buildscript {
ext {
springBootVersion = '1.3.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'org.springframework', name: 'spring-web', version: '4.0.0.RELEASE'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.12'
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.12'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.2.2'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.2.2'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.2.2'
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
Am I missing any dependency in build.gradle?
How to solve this?
You are including two different versions of the jackson lib. For Spring 4+ use the com.fasterxml.jackson.core like you have. Remove the two org.codehaus.jackson dependencies.

Categories

Resources