ClassNotFoundException when running main class in Intellij gradle project - java

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

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

Scala, Kotlin & Java Mixed Project failing on compileScala after migrating to gradle 4.0

I have a project that works fine on Gradle 3.5, but compiling scala fails with the Zinc compiler when it goes through the Java files. I know about the build caching that was added in 4.0, and overriding that functionality to have one output directory changed nothing. I attached my gradle file and the stack trace from gradle. Any suggestions on anything to try would be appreciated, I have hit a wall with this issue over the past couple of days.
group 'com.g'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.60'
repositories {
mavenCentral()
// maven {
// url "https://plugins.gradle.org/m2/"
// }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath ('org.springframework.boot:spring-boot-gradle-plugin:1.5.15.RELEASE')
// classpath "com.gradle:build-scan-plugin:1.15.2"
}
}
//apply plugin: "com.gradle.build-scan"
apply plugin: 'org.springframework.boot'
apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'scala'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
//tasks.compileJava.dependsOn compileScala
//tasks.compileScala.dependsOn.remove("compileJava")
sourceSets.main.scala.srcDirs = ['src/main/scala', 'src/main/java']
sourceSets.main.java.srcDirs = []
sourceSets.main.kotlin.srcDirs = ['src/main/kotlin', 'src/main/java']
//sourceSets.main.output.classesDir = new File(buildDir, "classes/main")
repositories {
jcenter()
mavenLocal()
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile 'org.slf4j:slf4j-api:1.7.5'
// zinc 'com.typesafe.zinc:zinc:0.3.9'
// zinc 'org.scala-lang:scala-library:' + scalaVersion
// zinc 'org.scala-lang:scala-reflect:' + scalaVersion
// zinc 'com.typesafe.zinc:zinc:0.3.7'
// scala
compile "org.scala-lang:scala-library:" + scalaVersion
compile "org.scala-lang:scala-reflect:" + scalaVersion
compile "org.scala-lang:scala-compiler:" + scalaVersion
// compile group: 'org.scala-lang', name: 'scala-library', version: '2.12.6'
// compile group: 'org.scala-lang', name: 'scala-reflect', version: '2.12.6'
// compile group: 'org.scala-lang', name: 'scala-compiler', version: '2.12.6'
// spark
compile('org.apache.spark:spark-mllib_2.11:2.3.1')
// compile 'org.apache.spark:spark-sql_2.11:2.1.0'
// compile 'org.apache.spark:spark-streaming-flume-assembly_2.11:2.1.0'
// compile 'org.apache.spark:spark-graphx_2.11:2.1.0'
// compile 'org.apache.spark:spark-launcher_2.11:2.1.0'
// compile 'org.apache.spark:spark-catalyst_2.11:2.1.0'
// compile 'org.apache.spark:spark-streaming_2.11:2.1.0'
compile 'org.apache.spark:spark-core_2.11:2.3.1'
// https://mvnrepository.com/artifact/org.codehaus.janino/commons-compiler
compile group: 'org.codehaus.janino', name: 'commons-compiler', version: '3.0.8'
compile group: 'org.codehaus.janino', name: 'janino', version: '3.0.8'
// postgres
compile group: 'org.postgresql', name: 'postgresql', version: '9.3-1104-jdbc4'
// hibernate
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.13.Final'
compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.13.Final'
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.13.Final'
compile group: 'org.hibernate', name: 'hibernate-envers', version: '5.2.13.Final'
// spring
compile group: 'org.springframework', name: 'spring-web', version: '5.0.8.RELEASE'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '2.0.9.RELEASE'
compile('org.springframework.boot:spring-boot-starter:1.5.15.RELEASE') {
exclude group: 'org.slf4j'
}
// open csv
compile group: 'com.opencsv', name: 'opencsv', version: '4.1'
// jersey
compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.19.4'
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.4'
// jbcrypt
compile group: 'org.mindrot', name: 'jbcrypt', version: '0.4'
// jetty
compile group: 'org.eclipse.jetty', name: 'jetty-servlet', version: '7.6.0.v20120127'
compile group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '7.6.0.v20120127'
//todo delete
compile group: 'com.wordnik', name: 'swagger-jaxrs_2.10', version: '1.3.0'
// log4j
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'commons-io', name: 'commons-io', version: '2.5'
}
task run(type: JavaExec, dependsOn: classes) {
main = mainClassFile
classpath sourceSets.main.runtimeClasspath
classpath configurations.runtime
}
task wrapper(type: Wrapper) {
gradleVersion = '4.0'
}
And the stack trace:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileScala'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88)
at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:242)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:317)
at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:309)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:185)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:95)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:235)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:224)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:121)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:77)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:102)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:96)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:612)
at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:567)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:96)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: org.gradle.api.internal.tasks.compile.CompilationFailedException: javac returned nonzero exit code
at org.gradle.api.internal.tasks.scala.ZincScalaCompiler$Compiler.execute(ZincScalaCompiler.java:83)
at org.gradle.api.internal.tasks.scala.ZincScalaCompiler.execute(ZincScalaCompiler.java:52)
at org.gradle.api.internal.tasks.scala.ZincScalaCompiler.execute(ZincScalaCompiler.java:38)
at org.gradle.api.internal.tasks.compile.daemon.AbstractDaemonCompiler$WorkerCompileSpec.compile(AbstractDaemonCompiler.java:74)
at org.gradle.api.internal.tasks.compile.daemon.AbstractDaemonCompiler$CompilerDaemonServer.execute(AbstractDaemonCompiler.java:82)
at org.gradle.api.internal.tasks.compile.daemon.AbstractDaemonCompiler$CompilerDaemonServer.execute(AbstractDaemonCompiler.java:78)
at org.gradle.process.internal.worker.request.WorkerAction.run(WorkerAction.java:91)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
... 3 more
Caused by: javac returned nonzero exit code
at sbt.compiler.JavaCompiler$JavaTool0.compile(JavaCompiler.scala:97)
at sbt.compiler.JavaTool$class.apply(JavaCompiler.scala:54)
at sbt.compiler.JavaCompiler$JavaTool0.apply(JavaCompiler.scala:84)
at sbt.compiler.JavaCompiler$class.compile(JavaCompiler.scala:35)
at sbt.compiler.JavaCompiler$JavaTool0.compile(JavaCompiler.scala:84)
at sbt.compiler.JavaCompiler$class.compileWithReporter(JavaCompiler.scala:40)
at sbt.compiler.JavaCompiler$JavaTool0.compileWithReporter(JavaCompiler.scala:84)
at sbt.compiler.AggressiveCompile$$anonfun$3$$anonfun$compileJava$1$1.apply$mcV$sp(AggressiveCompile.scala:121)
at sbt.compiler.AggressiveCompile$$anonfun$3$$anonfun$compileJava$1$1.apply(AggressiveCompile.scala:121)
at sbt.compiler.AggressiveCompile$$anonfun$3$$anonfun$compileJava$1$1.apply(AggressiveCompile.scala:121)
at sbt.compiler.AggressiveCompile.sbt$compiler$AggressiveCompile$$timed(AggressiveCompile.scala:168)
at sbt.compiler.AggressiveCompile$$anonfun$3.compileJava$1(AggressiveCompile.scala:120)
at sbt.compiler.AggressiveCompile$$anonfun$3.apply(AggressiveCompile.scala:142)
at sbt.compiler.AggressiveCompile$$anonfun$3.apply(AggressiveCompile.scala:84)
at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:66)
at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:64)
at sbt.inc.IncrementalCommon.cycle(IncrementalCommon.scala:32)
at sbt.inc.Incremental$$anonfun$1.apply(Incremental.scala:72)
at sbt.inc.Incremental$$anonfun$1.apply(Incremental.scala:71)
at sbt.inc.Incremental$.manageClassfiles(Incremental.scala:99)
at sbt.inc.Incremental$.compile(Incremental.scala:71)
at sbt.inc.IncrementalCompile$.apply(Compile.scala:54)
at sbt.compiler.AggressiveCompile.compile2(AggressiveCompile.scala:159)
at sbt.compiler.AggressiveCompile.compile1(AggressiveCompile.scala:68)
at com.typesafe.zinc.Compiler.compile(Compiler.scala:201)
at com.typesafe.zinc.Compiler.compile(Compiler.scala:183)
at com.typesafe.zinc.Compiler.compile(Compiler.scala:174)
at com.typesafe.zinc.Compiler.compile(Compiler.scala:165)
at org.gradle.api.internal.tasks.scala.ZincScalaCompiler$Compiler.execute(ZincScalaCompiler.java:81)
... 14 more

Remove all unnecessary libs from fat jar

I'm writing java console application using Spring Boot Jpa and MySQL connector. How I can easily exclude all unnecessary libs from my fat jar?
build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
// mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'ca.cutterslade.gradle:gradle-dependency-analyze:1.2.0'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'ca.cutterslade.analyze'
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
mysqlVersion = '6.0.6'
hibernateVersion = '5.2.12.Final'
}
repositories {
mavenCentral()
}
dependencies {
// compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.12.Final'
// compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.12.Final'
// compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
// compile group: 'org.springframework.boot', name: 'spring-boot', version: '1.5.9.RELEASE'
// compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.0.Final'
// compile group: 'org.springframework', name: 'spring-context', version: '4.3.13.RELEASE'
// compile group: 'org.springframework', name: 'spring-beans', version: '4.3.13.RELEASE'
// compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '1.5.9.RELEASE'
// compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
// compile group: 'org.springframework', name: 'spring-tx', version: '2.5.4'
// compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.9.RELEASE'
// testCompile group: 'org.springframework.boot', name: 'spring-boot-test', version: '1.5.9.RELEASE'
// testCompile group: 'junit', name: 'junit', version: '4.12'
// testCompile group: 'org.springframework', name: 'spring-test', version: '4.3.13.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: mysqlVersion
compile group: 'org.hibernate', name: 'hibernate-core', version: hibernateVersion
compile("org.springframework.boot:spring-boot-starter-data-jpa")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
jar {
baseName 'ReportGenerator'
version '1.0'
}
UPDATE
I have tried to use gradle-dependency-analyze and received the following result:
usedUndeclaredArtifacts:
- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final
- org.springframework.boot:spring-boot:1.5.9.RELEASE
- org.springframework:spring-context:4.3.13.RELEASE
- org.springframework:spring-beans:4.3.13.RELEASE
- org.springframework.boot:spring-boot-autoconfigure:1.5.9.RELEASE
- org.slf4j:slf4j-api:1.7.25
- org.springframework:spring-tx:4.3.13.RELEASE
- org.springframework.data:spring-data-jpa:1.11.9.RELEASE
unusedDeclaredArtifacts:
- mysql:mysql-connector-java:6.0.6
- org.hibernate:hibernate-core:5.2.12.Final
- org.springframework.boot:spring-boot-starter-data-jpa:1.5.9.RELEASE
As you see unused libraries are marked all libraries in my gradle at the moment. And required librariesare marked only which are used in the scope of my classes in app but don't get me know what these required libraries depends on also. If I will put only these list of required dependencies in the gradle then I will get different initialization errors since some libraries are missed.
Couple of suggestions.
Use dependency:analyze on your project which will list the unused dependencies, which you can exclude or get rid of.
use the <scope> attribute for each and every dependency in your pom, which can greatly reduce your fat jar size. Provide correct scope parameters, like compile, test .....

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

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

Categories

Resources