NoClassDefFoundError: MappingJackson2HttpMessageConverter in Spring/REST Tutorial - java

I am trying to build and run this tutorial "Designing and Implementing RESTful Web Services with Spring" off-line, using a local repository for my dependencies. Here is my gradle.build file:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
repositories {
flatDir { dirs 'lib' }
}
dependencies {
compile 'org.springframework:spring-core:4.0.6.RELEASE'
compile 'org.springframework:spring-beans:4.0.6.RELEASE'
compile 'org.springframework:spring-context:4.0.6.RELEASE'
compile 'org.springframework:spring-web:4.0.6.RELEASE'
compile 'org.springframework:spring-webmvc:4.0.6.RELEASE'
compile 'com.jayway.jsonpath:json-path:0.8.1'
compile 'org.slf4j:slf4j-api:1.7.5'
runtime 'org.slf4j:slf4j-jdk14:1.7.5'
// {!begin jackson}
runtime 'com.fasterxml.jackson.core:jackson-core:2.4.2'
runtime 'com.fasterxml.jackson.databind:jackson-databind:2.4.2'
runtime 'com.fasterxml.jackson.annotation:jackson-annotations:2.4.2'
// {!end jackson}
// {!begin jaxb}
runtime 'javax.xml.bind:jaxb-api:2.2.9'
// {!end jaxb}
// {!begin commons}
runtime 'org.apache.commons:commons-logging:1.1.1'
// {!end commons}
testCompile 'org.springframework:spring-test:4.0.6.RELEASE'
testCompile 'junit:junit:4.+'
testCompile "org.mockito:mockito-all:1.9.5"
testCompile "org.hamcrest:hamcrest-all:1.3"
testCompile 'javax.servlet:javax.servlet-api:3.0.1'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.6'
}
Some of the tests fail with the following error:
java.lang.NoClassDefFoundError: Could not initialize class org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
This class is in the spring-web-4.0.6.RELEASE.jar, which is in the dependencies. What am I missing?

Related

Error in executing the Jar file in command Pompt (Gradle Build): NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook

I am building a Java Project using Gradle Build. The project has some external dependencies like Apache POI, Spring Core/Context, Log4j. When I run the project in my IDE , it is compiling and executing successfully without any errors.
However, when I build the Jar file using gradle build and run it in command prompt (using "java -jar file-name") I get Error -
NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
Here is the my build.gradle file -
buildscript{
repositories{
mavenCentral()
}
dependencies{
classpath group: 'org.springframework', name: 'spring-core', version: '5.0.2.RELEASE'
classpath group: 'org.springframework', name: 'spring-context', version: '5.0.2.RELEASE'
classpath group: 'org.apache.poi', name: 'poi', version: '3.17'
classpath group: 'org.apache.poi', name: 'poi-ooxml', version: '3.17'
classpath group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '3.17'
classpath group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
classpath group: 'org.apache.xmlbeans', name: 'xmlbeans', version: '2.6.0'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
mainClassName = '<package>.HelloWorld'
dependencies{
compile 'org.apache.poi:poi:3.17'
compile 'org.apache.poi:poi-ooxml:3.17'
compile 'org.apache.poi:poi-ooxml-schemas:3.17'
compile 'org.springframework:spring-core:5.0.2.RELEASE'
compile 'org.springframework:spring-context:5.0.2.RELEASE'
compile 'org.apache.commons:commons-collections4:4.1'
compile 'org.apache.xmlbeans:xmlbeans:2.6.0'
}
jar {
manifest{
attributes '<package>.HelloWorld'
}
baseName = 'finder-app'
version = '0.1.0'
}
The dependency jar files are present on my local machine.
Do I need to add the jar files when I package the jar using build.gradle? If yes, then how? OR
Do I need to add CLASSPATH (to external jar directory) to my local machine environment variable?
You can try the following build.gradle, it would deliver a uber jar, which package all the dependencies (doc reference).
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
jcenter()
}
dependencies {
compile 'com.google.guava:guava:23.0'
testCompile 'junit:junit:4.12'
}
mainClassName = "com.github.Library"
dependencies{
compile 'org.apache.poi:poi:3.17'
compile 'org.apache.poi:poi-ooxml:3.17'
compile 'org.apache.poi:poi-ooxml-schemas:3.17'
compile 'org.springframework:spring-core:5.0.2.RELEASE'
compile 'org.springframework:spring-context:5.0.2.RELEASE'
compile 'org.apache.commons:commons-collections4:4.1'
compile 'org.apache.xmlbeans:xmlbeans:2.6.0'
}
shadowJar {
baseName = 'finder-app'
version = '0.1.0'
}
Just run ./gradlew :shadowJar, and then you can run java -jar build/libs/finder-app-0.1.0-all.jar. It would work for your favor.
NOTE, you dont need so many irrelevant dependencies in your buildscript{}, those are serves for plugins usage.
To run a java application you need to provide a classpath
You have two options:
provide the classpath in the jar manifest (META-INF/manifest) then you can run your jar using
java -jar myjar.jar
provide the classpath using -cp parameter then you can run your jar using
java -cp list_of_dependencies_jar_and_main_jar package.MyMainClass

how to exclude a folder from checkstyle of Gradle on Intellij

I am new for IntelliJ and Gradle, but I have enough experience on Java and Eclipse. I generated some java classes from wsdl file. I put them under src/main/resources with a folder name - "generatedsources".
I try to prevent checkstyle for this folder and its subfolders like src/main/resources/generatedsources/* with gradle on IntelliJ.
I tried some lines such;
task checkstyle(type: Checkstyle) {
source 'src'
// include '**/*.java'
// exclude '**/gen/**'
// exclude '**/R.java'
// exclude '**/BuildConfig.java'
exclude 'src/main/resources/generatedsources/**'
}
But I'm failed again.
build.gradle;
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
sourceCompatibility = 1.7
version = '1.0'
...
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
task checkstyle(type: Checkstyle) {
source 'src'
// include '**/*.java'
// exclude '**/gen/**'
// exclude '**/R.java'
// exclude '**/BuildConfig.java'
exclude 'src/main/resources/generatedsources/**'
}
EDIT - After recommendations(but still failed!):
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
sourceCompatibility = 1.7
version = '1.0'
description = """BLABLABLA_application"""
war.archiveName = "BLABLABLA.war"
configurations{
deployerJars
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.3'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.3'
compile group: 'org.springframework', name: 'spring-core', version:'4.0.0.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version:'4.0.0.RELEASE'
compile 'log4j:log4j:1.2.17'
compile 'com.sun.xml.ws:jaxws-rt:2.2.8'
compile 'org.jvnet.jax-ws-commons.spring:jaxws-spring:1.9'
compile group: 'org.springframework', name: 'spring-webmvc', version:'4.0.0.RELEASE'
providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
deployerJars "org.apache.maven.wagon:wagon-http:2.2"
}
jar {
manifest {
attributes 'Implementation-Title': 'BLABLABLA', 'Implementation-Version': version
}
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
wsdl2java{
wsdlsToGenerate = [
["$projectDir/src/main/resources/BLABLABLA.wsdl"]
]
generatedWsdlDir = file("$projectDir/src/gen/java")
wsdlDir = file("$projectDir/src/main/resources")
}
wsdl2javaExt {
cxfVersion = "2.5.1"
}
tasks.withType(Checkstyle) {
exclude '**/your/generated/package/goes/here**'
}
checkstyleMain.exclude '**/your/generated/package/goes/here**'
"exclude" in tasks.withType and "checkstyleMain" causes an error such as "cannot resolve symbol"!
When checkstyle plugin is applied custom tasks are delivered along with it (see here), so instead of adding custom task with type Checkstyle configure the shipped one. This is how it should be done:
tasks.withType(Checkstyle) {
exclude '**/your/generated/package/goes/here**'
}
You can also configure a particular task, not all:
checkstyleMain.exclude '**/your/generated/package/goes/here**'
Also this is not good practice to put generated sources under src/main/resources. Typically such files goes to e.g. src/gen/java
I followed some comments and make it work add this in file build.gradle
checkstyle {
config = rootProject.resources.text.fromFile("${project.projectDir}/checkstyle-8.34_google_checks.xml")
toolVersion = '8.34'
ignoreFailures = false
maxWarnings = 0
}
checkstyleMain
.exclude('com/examples/gen/api/*.java')
.exclude('com/examples/gen/model/*.java')
.exclude('com/examples/gen/auth/*.java')
You can only exclude a folder that is in the source set. That is something that had not been mentioned and it caused me to struggle since I am using Intellij. The generated files are in the build/ folder which is on the same level as src/. I tried excluding the build folder for a while but there was no effect. The only solution was to specify the source folder. Since the build folder is not under src (the source folder), it worked immediately.
checkstyleMain.source = "src/main/java"
I found the solution here: Specify excludes to Checkstyle task
I had lots of issues with checkStyle & pmd + QueryDSL
A final solution for me was not to exclude generated sources, but clean them after assemble task: no sources - nothing to analyze! :)
assemble {
finalizedBy cleanQuerydslSourcesDir
}
For kotlin dsl use
tasks.withType<Checkstyle>() {
exclude("**/com/generated/*.java")
}

How to configure Quasar in Gradle

I'm new to Gradle and I don't know what to do.
Here is Quasar docs about how to install Quasar through Gradle: Quasar Docs
There is also a template project in the page: Template Gradle Project
Finally this is my build.gradle:
group 'TGAdminsBot'
version '0.1'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
mainClassName = "Launcher"
idea {
module
{
downloadJavadoc = true
downloadSources = true
}
}
dependencies {
compile 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.4'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.4'
//compile 'com.github.User:Repo:Tag'
//compile 'com.mashape.unirest:unirest-java:1.4.9'
compile 'co.paralleluniverse:comsat-httpclient:0.7.0'
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.2.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
configurations {
quasar
}
task runQuasar {
jvmArgs "-javaagent:${configurations.quasar.iterator().next()}"
}
run.dependsOn runQuasar
And I get this Error:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\Sobhan\Documents\IntelliJIDEAProjects\TGAdminsBot\build.gradle' line: 39
* What went wrong:
A problem occurred evaluating root project 'TGAdminsBot'.
> java.util.NoSuchElementException (no error message)
So What should I do? I'm again sorry to ask this question but I'm new to Gradle and I Googled so much before posting this question. Thanks
There were three problems.
configurations were defined before dependencies.
Two lines were needed in dependencies:
compile 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
quasar 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
Lack of this block:
tasks.withType(JavaExec){
jvmArgs "-javaagent:${configurations.quasar.iterator().next()}"
}
Finally this is final build.gradle:
group 'TGAdminsBot'
version '0.1'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
idea {
module
{
downloadJavadoc = true
downloadSources = true
}
}
configurations {
quasar
}
dependencies {
compile 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
quasar 'co.paralleluniverse:quasar-core:0.7.4:jdk8'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.4'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.4'
compile 'co.paralleluniverse:comsat-httpclient:0.7.0'
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.2.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
tasks.withType(JavaExec)
{
jvmArgs "-javaagent:${configurations.quasar.iterator().next()}"
}
task run(type: JavaExec) {
main = 'com.sunova.bot.Launcher'
classpath = sourceSets.main.runtimeClasspath
}
I think your problem lies mostly in the definition of runQuasar which is not a run task and thus has no jvmArgs property but, if you don't need it for other reasons I'm unaware of, just do as in the Gradle template project (agent configuration) rather than defining runQuasar and declaring that run depends on it:
applicationDefaultJvmArgs = [
"-javaagent:${configurations.quasar.singleFile}" // =v, =d
]
If you need a separate runQuasar I think you'll need to declare it as a JavaExec task (have a look here).

How can I add a generated source folder to my source path in Gradle and IntelliJ?

I use thrift and it generates some source java files(interfaces) under build directory (build/generated-sources/thrift/<package name>/<class>) but under my src/main/java I have my classes which has the same package definition as in the generated java files and my classes also implements the interfaces generated by the thrift so how can I configure this in my build.gradle so it works on intelliJ as well as the build
plugins {
id "org.jruyi.thrift" version "0.3.1"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "org.jruyi.thrift"
group 'com.hello'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.thrift', name: 'libthrift', version:'0.9.3'
compile 'com.datastax.cassandra:cassandra-driver-core:3.0.0'
compile 'com.datastax.cassandra:cassandra-driver-mapping:3.0.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
compileThrift {
thriftExecutable "/usr/local/hello/bin/thrift"
sourceDir "src/main/thrift"
createGenFolder false
}
task thrift(type: Exec) {
commandLine '/usr/local/hello/bin/thrift'
}
compileJava {
dependsOn 'compileThrift'
The gradle build should work automatically.
To make it work on Intellij, try adding the following to your build.gradle.
idea.module.sourceDirs += file("$buildDir/generated-sources/thrift")
Don't forget to refresh your gradle projects.

Plugin with id 'jacoco' not found + Gradle

I'm trying to add jacoco support to my gradle project, but when I add the jacoco plugin, it gives me an error.
Here is my gradle.build
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
apply plugin: 'application'
apply plugin: 'project-report'
apply plugin: 'jacoco'
eclipse {
classpath { downloadSources=true }
}
eclipse.classpath.file {
// Classpath entry for Eclipse which changes the order of classpathentries; otherwise no sources for 3rd party jars are shown
withXml { xml ->
def node = xml.asNode()
node.remove( node.find { it.#path == 'org.eclipse.jst.j2ee.internal.web.container' } )
node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.jst.j2ee.internal.web.container', exported: 'true'])
}
}
tasks.withType(Compile) { options.encoding = 'UTF-8' }
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
providedCompile 'javax.servlet:servlet-api:2.5'
runtime 'javax.servlet:jstl:1.1.2'
compile 'org.springframework.batch:spring-batch-core:2.2.5.RELEASE'
compile 'org.springframework:spring-webmvc:4.0.2.RELEASE'
compile 'org.springframework:spring-jdbc:4.0.2.RELEASE'
compile 'org.springframework:spring-orm:4.0.2.RELEASE'
compile 'org.springframework.data:spring-data-mongodb:1.4.0.RELEASE'
compile 'org.springframework.security:spring-security-web:3.2.1.RELEASE'
compile 'org.springframework.security:spring-security-config:3.2.1.RELEASE'
compile 'org.slf4j:slf4j-simple:1.6.1'
compile 'org.codehaus.groovy:groovy-all:2.2.0'
compile 'org.mongodb:mongo-java-driver:2.11.4'
compile 'c3p0:c3p0:0.9.1.2'
compile 'org.hibernate:hibernate-core:4.3.4.Final'
compile 'org.hibernate:hibernate-ehcache:4.3.4.Final'
compile 'org.hsqldb:hsqldb:2.0.0'
compile 'com.google.guava:guava:16.0'
compile 'commons-io:commons-io:2.4'
compile 'com.google.code.gson:gson:2.2.4'
compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
testCompile 'junit:junit:4.11'
testCompile 'commons-collections:commons-collections:3.2'
testCompile 'org.springframework:spring-test:4.0.2.RELEASE'
testCompile 'org.codehaus.groovy:groovy-all:2.2.0'
testCompile 'de.flapdoodle.embed:de.flapdoodle.embed.mongo:1.35'
testCompile 'org.springframework.batch:spring-batch-test:2.2.5.RELEASE'
compile localGroovy()
}
test {
testLogging { // Show that tests are run in the command-line output
events 'started', 'passed' }
exclude 'com/bambilon/All*'
exclude 'com/bambilon/**/slow/*'
}
and when I run refresh dependencies in Eclipse, it gives me this error:
Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin with id 'jacoco' not found.
at org.gradle.api.internal.plugins.DefaultPluginRegistry.getTypeForId(DefaultPluginRegistry.java:86)
at org.gradle.api.internal.plugins.DefaultProjectsPluginContainer.getTypeForId(DefaultProjectsPluginContainer.java:102)
at org.gradle.api.internal.plugins.DefaultProjectsPluginContainer.apply(DefaultProjectsPluginContainer.java:37)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.applyPlugin(DefaultObjectConfigurationAction.java:101)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.access$200(DefaultObjectConfigurationAction.java:32)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction$3.run(DefaultObjectConfigurationAction.java:72)
at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.execute(DefaultObjectConfigurationAction.java:114)
at org.gradle.api.internal.project.AbstractProject.apply(AbstractProject.java:846)
at org.gradle.api.Project$apply.call(Unknown Source)
at org.gradle.api.internal.project.ProjectScript.apply(ProjectScript.groovy:34)
at org.gradle.api.Script$apply.callCurrent(Unknown Source)
Please help, thanks!
I am not quite sure which version introdueced jacoco plugin but with Gradle 2.x it for sure works. If you have a $buildDir/jacoco folder after running gradle build, you can be sure that it works. More information on http://www.gradle.org/docs/current/userguide/jacoco_plugin.html.
I have also got the same problem , I have set the gradle path in eclipse .It sloved for me.To set the path
Eclipse -Window->preference-type Gardle
Click on folder radio button , and set path to gradle folder located in your system.

Categories

Resources