NoClassDefFoundError with gradle multi projects - java

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'
}
}

Related

Not able to reference other modules inside gradle project in intelij

I have created a relatively new project with 4 modules inside one project (created with intelij). However, I have now got to the stage where I want to import one module (:domain) from another (app-updater). The issue I'm facing is when I do this from within intelij it gives me errors in the "Build: Sync" tab. This is strange since my module app-updater is able to reference to module domain when I add the line "compile project(':domain')" into build.gradle and when I remove the line I get issues with the class being missing so the build: sync seems to be working it's just running it from inside intelij seems to give an error saying "Project with path ':domain' could not be found in root project 'app-updater'." I have attached my gradle config below, hopefully I have given enough info but if not feel free to ask for more, thanks for any help.
Main project settings.gradle file
rootProject.name = 'rGuide'
include 'domain'
include 'service'
include 'app-updater'
include 'app-rest'
:domain build.gradle
group 'domain'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.9'
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
app-updater build.gradle file
group 'app-updater'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
google()
maven {
url 'https://repo.spring.io/plugins-release/'
}
maven {
url 'https://repo.spring.io/milestone'
}
}
dependencies {
compile project(':domain')
// https://mvnrepository.com
compile group: 'net.ser1', name: 'gozirra-client', version: '0.4.1'
compile group: 'io.netty', name: 'netty-all', version: '4.1.36.Final'
compile group: 'io.projectreactor.netty', name: 'reactor-netty', version: '0.9.0.M2'
compile group: 'org.springframework.integration', name: 'spring-integration-stomp', version: '5.1.6.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-core', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-websocket', version: '5.1.6.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.9'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
TLDR - Trying to import domain into app-updater within intelij, it "works fine" and seems to import domain however when I do this with the Build: sync tab in intelij it gives an error saying "Project with path ':domain' could not be found in root project 'app-updater'.". Thanks for any help!!

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

exported runnable jar doesn't contain dependency jars from eclipse 2018-12 with java 11. How do we solve this?

My byild.gradle file
Able to export jar with JAVA 8. when I configure to JAVA 11 exported jar doesn't contain external jars
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
}
configurations
{
all*.exclude group: 'org.slf4j', module: 'slf4j-log4j12' //by both name and group
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:21.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile group: 'com.microsoft', name: 'sqljdbc4', version: '3.0'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
compile group: 'com.ibatis', name: 'ibatis2-common', version: '2.1.7.597'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
compile group: 'org.json', name: 'json', version: '20171018'
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13'
compile group: 'com.itextpdf.tool', name: 'xmlworker', version: '5.5.13'
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.2'
}
add this below lines into your build.gradle
jar { manifest {attributes "Main-Class": "your main class" } from {configurations.compile.collect { it.isDirectory()? it : zipTree(it) } }
then do a clean/build,
the jar created in \build\libs folder is your runnable jar

java.lang.NoClassDefFoundError: when trying to run jar

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

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

Categories

Resources