I use Java + Gradle (embedded in intellij idea) ('gradle' = 'sh gradlew')

In the editor or when running the gradle run everything works, but when I try to compile the gradle build and run java -jar ./build/libs/LinearServer-1.0-SNAPSHOT.jar I get the Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger error Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger when calling Logger.getLogger(Main.class);

Code build.gradle file:

 plugins { id 'java' id 'application' } group 'ru.screamt5.linear' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 mainClassName = 'ru.screamt5.linear.LinearServer.Main' repositories { mavenCentral() } dependencies { compile group: 'commons-cli', name: 'commons-cli', version: '1.4' compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2' compile group: 'com.google.inject', name: 'guice', version: '4.2.0' compile group: 'log4j', name: 'log4j', version: '1.2.17' testCompile group: 'junit', name: 'junit', version: '4.12' } jar { manifest { attributes( 'Main-Class': mainClassName, 'Class-Path': configurations.compile.collect { it.getName() }.join(' ') ) } } 

What could be the problem?

  • one
    Because your startup team is looking for classes only in LinearServer-1.0-SNAPSHOT.jar. It is necessary either to assemble a fat jar containing all dependencies, or to put all dependencies in CLASSPATH and point them out at launch. - Sergey Gornostaev

1 answer 1

It is necessary to add from to jar

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