Spring can not find apache commons logging - java

Java 1.8
Spring 5.3.5
Tomcat 9 in Docker
Trying to run Spring app inside apache tomcat in docker container, but Spring fails to initialize:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
erMapping]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: 'org.apache.commons.logging.Log
org.springframework.core.log.LogDelegateFactory.getHiddenLog(java.lang.String)' at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:225)
I have included apache commons into dependencies:
plugins {
id 'war'
id 'org.springframework.boot' version '2.5.0-M2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
dependencies {
implementation group: 'org.springframework', name: 'spring-jcl', version: '5.3.5'
implementation group: 'org.springframework', name: 'spring', version: '5.3.5', ext: 'pom'
implementation group: 'org.springframework', name: 'spring-webmvc', version: '5.3.5'
implementation group: 'commons-logging', name: 'commons-logging', version: '1.2'
implementation 'org.slf4j:jcl-over-slf4j:1.7.3'
implementation 'ch.qos.logback:logback-classic:1.2.3'}
I can also see it in when deployed (thats how it looks on the server after deployment in tomcat9):
Any idea what is wrong?

Ok I hope it will help someone. It turned out that this plugin messed it all up:
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
It was added by default by Spring initializer web app. After I removed it, everything started to work as it should.

Related

I get a error: java.lang.NoClassDefFoundError

I need help. My dependency is following:
compile group: 'org.springframework.cloud', name: 'spring-cloud-stream-binder-kinesis', version: '2.0.1.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '3.0.2.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework:spring-context-support'
I get the error:
java.lang.NoClassDefFoundError: org/springframework/web/context/support/StandardServletEnvironment
Is there something wrong with my dependency?
it's in spring-web, which is a dependency of spring-boot-starter-web. Here you've overridden spring-web with a very old Spring Framework version. That's the source of your problem.

Apache Camel Timer Nullpointer exception at Fat Jar

I'm trying to implement a timer using Camel 3.5 at Gradle project with OpenJDK8 as next
from("timer://watchexpiration?fixedRate=true&period=600000&delay=0")...
But, after build the fat jar using ./gradlew build and run as java -jar build/libs/app.jar
I receive the next error at console
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: timer://watchexpiration?delay=0&fixedRate=true&period=600000 due to: Error binding property (delay=0) with name: delay on bean: timer://watchexpiration?delay=0&fixedRate=true&period=600000 with value: 0
at org.apache.camel.impl.engine.AbstractCamelContext.doGetEndpoint(AbstractCamelContext.java:888)
at org.apache.camel.impl.engine.AbstractCamelContext.getEndpoint(AbstractCamelContext.java:777)
at org.apache.camel.support.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:58)
at org.apache.camel.reifier.AbstractReifier.resolveEndpoint(AbstractReifier.java:177)
at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:250)
at org.apache.camel.reifier.RouteReifier.createRoute(RouteReifier.java:112)
But If I run using ./gradlew run then works fine as I expected.
I don't want to use any frameworks for this project. I feel this is just a config issue or something is wrong with my configuration I guess.
How can I fix it?
build.gradle
plugins {
id 'java'
id 'application'
id 'com.github.sherter.google-java-format' version '0.8'
}
repositories {
jcenter()
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
// Camel
compile group: 'org.apache.camel', name: 'camel-core', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-file', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-file-watch', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-xstream', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-gson', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-rest', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-http', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-quartz', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-timer', version: '3.5.0'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
// Dev Libs
compileOnly("org.projectlombok:lombok:1.18.12")
annotationProcessor("org.projectlombok:lombok:1.18.12")
compile group: 'org.apache.commons', name: 'commons-csv', version: '1.4'
}
application {
mainClassName = 'com.eip.App'
}
configurations {
// configuration that holds jars to include in the jar
extraLibs
}
jar {
manifest {
attributes(
'Main-Class': 'com.beam.agent.App'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
test {
useJUnitPlatform()
}
googleJavaFormat {
exclude '**/App.java'
}
Shadowing jars can be tricky, because you need to handle duplicate entries. In Apache Camel there are many META-INF service files, which are getting overwritten with your simple jar approach. Use com.github.johnrengelman.shadow which is allowing you to customize the merging process.
plugins {
id 'java'
id 'application'
id 'com.github.sherter.google-java-format' version '0.8'
id "com.github.johnrengelman.shadow" version "6.0.0" // Added plugin
}
repositories {
jcenter()
}
dependencies {
// ...
}
application {
mainClassName = 'com.eip.App'
}
// Removed jar step
test {
useJUnitPlatform()
}
googleJavaFormat {
exclude '**/App.java'
}
// Added shadow plugin configuration
shadowJar {
mergeServiceFiles() // Tell plugin to merge duplicate service files
manifest {
attributes 'Main-Class': 'com.eip.App'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
Shaded executable jar will have suffix -all.jar
java -jar build/libs/app-all.jar

Dropwizard using jakarta not javax

I am currently developing a webservice in java using the framework Dropwizard.
For the Rest API interface I was using JAX-RS. Since I needed to upload files with a POST-Request I added jersey-media-multipart to my Gradle dependencies. However suddenly intellij could not resolve the javax.ws.rs.* import statements but the newer jakarta.ws.rs and thus I changed the interface to jakarta. The problem now is that Dropwizard apparently still needs JAX-RS and so I get the following error with stacktrace:
java.lang.NoClassDefFoundError: javax/ws/rs/QueryParam
at io.dropwizard.jersey.validation.JerseyParameterNameProvider.getParameterNameFromAnnotations(JerseyParameterNameProvider.java:46)
at io.dropwizard.jersey.validation.JerseyParameterNameProvider.getParameterNames(JerseyParameterNameProvider.java:35)
at org.hibernate.validator.internal.util.ExecutableParameterNameProvider.getParameterNames(ExecutableParameterNameProvider.java:37)
at org.hibernate.validator.internal.properties.javabean.JavaBeanExecutable.getParameterName(JavaBeanExecutable.java:86)
at org.hibernate.validator.internal.metadata.aggregated.ParameterMetaData$Builder.build(ParameterMetaData.java:165)
at org.hibernate.validator.internal.metadata.aggregated.ExecutableMetaData$Builder.findParameterMetaData(ExecutableMetaData.java:436)
at org.hibernate.validator.internal.metadata.aggregated.ExecutableMetaData$Builder.build(ExecutableMetaData.java:391)
at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataBuilder$BuilderDelegate.build(BeanMetaDataBuilder.java:260)
at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataBuilder.build(BeanMetaDataBuilder.java:133)
at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.createBeanMetaData(BeanMetaDataManagerImpl.java:206)
at org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl.getBeanMetaData(BeanMetaDataManagerImpl.java:165)
at org.hibernate.validator.internal.engine.ValidatorImpl.buildNewLocalExecutionContext(ValidatorImpl.java:772)
at org.hibernate.validator.internal.engine.ValidatorImpl.access$200(ValidatorImpl.java:84)
at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.doValidate(ValidatorImpl.java:707)
at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.indexedValue(ValidatorImpl.java:681)
at org.hibernate.validator.internal.engine.valueextraction.ListValueExtractor.extractValues(ListValueExtractor.java:26)
at org.hibernate.validator.internal.engine.valueextraction.ListValueExtractor.extractValues(ListValueExtractor.java:16)
at org.hibernate.validator.internal.engine.valueextraction.ValueExtractorHelper.extractValues(ValueExtractorHelper.java:42)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedContainerElementsForCurrentGroup(ValidatorImpl.java:651)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:598)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:409)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedAnnotatedObjectForCurrentGroup(ValidatorImpl.java:629)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:590)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:409)
at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:172)
at io.dropwizard.configuration.BaseConfigurationFactory.validate(BaseConfigurationFactory.java:238)
at io.dropwizard.configuration.BaseConfigurationFactory.build(BaseConfigurationFactory.java:127)
at io.dropwizard.configuration.BaseConfigurationFactory.build(BaseConfigurationFactory.java:108)
at io.dropwizard.cli.ConfiguredCommand.parseConfiguration(ConfiguredCommand.java:128)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:74)
at io.dropwizard.cli.Cli.run(Cli.java:78)
at io.dropwizard.Application.run(Application.java:94)
at Application.main(Application.java:10)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.QueryParam
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.QueryParam
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 33 more
Execution failed for task ':Application.main()'.
> Process 'command '/usr/lib/jvm/java-13-openjdk/bin/java'' finished with non-zero exit value 1
I dont know if my build.gradle helps resolving my problem but here is it with my dependencies:
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'application'
dependencies {
compile 'io.dropwizard:dropwizard-core:2.0.8'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-multipart', version: '3.0.0-M1'
}
mainClassName = 'Application'
run {
args = ['server']
}
I am pretty helpless and googled a lot about dropwizard and found out that they apparently should support jakarta since version 2.0.0. I am currently using version 2.0.8 but still getting this internal bug.
I fixed it using the own dropwizard library dropwizard-forms and not the jersey-media-multipart.
My new gradle dependencies now look like this:
dependencies {
compile group: 'io.dropwizard', name: 'dropwizard-core', version: '2.0.8'
compile 'io.dropwizard:dropwizard-forms:2.0.8'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Now I can also use javax again and do not need to switch to jakarta.

Unresolved reference when referencing Java code from Kotlin tests in Spring Boot project using Gradle

I have a project where I am using both Kotlin and Java code. My main Kotlin code (including the #SpringBootApplication main class) are in src/main/kotlin, but there is some legacy client library code which is in src/main/java. I also have some tests in src/test/kotlin.
In IntelliJ I can run the tests without issue, but when I run the test using gradle test I get errors Unresolved reference and cannot access class **. I am unsure why this is happening. The errors related to code in the Java classes.
My kotlin code and tests are in a slightly different package to the client library code; my main code is in a package called com.sky.vision.playlistapi and the client library is in a package called com.sky.nifty. I wonder if this could be the root of the problem?
My build.gradle file is here:
buildscript {
ext {
kotlinVersion = '1.2.10'
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '1.5.9.RELEASE'
id 'org.jetbrains.kotlin.jvm' version'1.2.10'
id 'org.jetbrains.kotlin.plugin.allopen' version '1.2.10'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.10'
id 'org.jetbrains.kotlin.plugin.jpa' version '1.2.10'
id 'org.jetbrains.kotlin.plugin.noarg' version '1.2.10'
}
group = 'com.sky.vision'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(group: 'org.springframework.boot', name: 'spring-boot-starter')
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web')
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-security')
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa')
compile(group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: kotlinVersion)
compile(group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: kotlinVersion)
compile(group: 'org.apache.commons', name: 'commons-lang3', version: '3.4')
compile(group: 'com.fasterxml.jackson.core', name: 'jackson-core')
compile(group: 'com.fasterxml.jackson.core', name: 'jackson-databind')
compile(group: 'org.reflections', name: 'reflections', version: '0.9.9')
compile(group: 'org.aspectj', name: 'aspectjweaver')
compile(group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7')
testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test')
testCompile(group: 'org.jetbrains.kotlin', name: 'kotlin-test')
testCompile(group: 'org.jetbrains.kotlin', name: 'kotlin-test-junit')
runtime(group: 'org.postgresql', name: 'postgresql', version: '9.4-1201-jdbc41')
}
And here is my project layout:
If anyone is able to help me understand the problem I would be grateful.
It turns out the problem was that I had some .java classes inside src/main/kotlin package. Once I moved these under src/main/java package the problem was resolved.

Gretty: Duplicate fragment name: org_apache_jasper

I use gretty to easily run a dev server and webapp-runner for deployment to heroku.
The following is my gradle.build:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.akhikhl.gretty:gretty:+'
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'org.akhikhl.gretty'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.springframework:spring-webmvc:4.3.10.RELEASE'
compile 'org.springframework:spring-orm:4.3.10.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.0'
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.10.Final'
compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.1.Final'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
compile 'com.github.jsimone:webapp-runner:8.5.11.3'
}
gretty {
httpPort = 8080
servletContainer = 'jetty9'
contextPath = '/'
}
eclipse {
wtp {
component {
contextPath = '/'
}
}
}
///////// Tasks for deployment to heroku
task stage() {
dependsOn clean, war
}
war.mustRunAfter clean
task copyToLib(type: Copy) {
dependsOn war
into "$buildDir/server"
from(configurations.compile) {
include "webapp-runner*"
}
}
stage.dependsOn(copyToLib)
If I remove webapp-runner everything runs fine, but with it I get the following error when trying to start gretty:
java.lang.IllegalStateException: Duplicate fragment name: org_apache_jasper for jar
Not an expert but I figure its something to do with the fact that both gretty and webapp-runner download similar files and that's causing a clash?
Would really appreciate some info on this. How do I get past this? Is there a better way to have a dev server + be able to deploy to heroku? (maybe use webapp-runner for both?)
I recommend running locally the same way as Heroku runs your app, with these commands:
$ ./gradlew stage
$ heroku local web
If you want to use gretty for development, you'll need to exclude webapp-runner from your dev build (maybe with a stageDev task), and exclude gretty from your stage build.

Categories