Dropwizard using jakarta not javax - java

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.

Related

Spring can not find apache commons logging

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.

SPRING BOOT-KOTLIN: importing a custom java class to a (Kotlin) controller raises java.lang.NoClassDefFoundError [duplicate]

As the title says, I am trying to mix Java and Kotlin in a single project. There's a good example found here. mixed-java-kotlin-hello-world. Everything is working properly besides kotlin cannot find any of my Java classes which are found in src/main/java/somepackage/SomeClass.java
What would be the cause of this?
This is the error I am getting.
When I try to edit my build.gradle, it shows I'm not spelling kotlin correctly and giving me errors on plugin but I'm copying and pasting from kotlins website.
My build.gradle looks like this
group 'Battle-OS'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.2-1"
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.2-1"
testCompile 'junit:junit:4.11'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:1.0.2-1"
compile 'org.slf4j:slf4j-api:1.7.18'
testCompile 'junit:junit:4.12'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
// https://mvnrepository.com/artifact/com.google.guava/guava
compile group: 'com.google.guava', name: 'guava', version: '19.0'
// https://mvnrepository.com/artifact/io.netty/netty-all
compile group: 'io.netty', name: 'netty-all', version: '4.0.37.Final'
// https://mvnrepository.com/artifact/com.moandjiezana.toml/toml4j
compile group: 'com.moandjiezana.toml', name: 'toml4j', version: '0.6.0'
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.0.2-1'
}
Instead of starting off trying to make this work on a larger project. I tried to make this work by creating a new gradle project. I created a test java class which contains a method to print hello world. Then I created a test kotlin class which creates a new object of the java class and calls the method from the java class to print hello world.
This solves the problem I had above, now I can call java classes from kotlin and kotlin from java but now it gives me errors upon running it.
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at AKotlinClassKt.main(AKotlinClass.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
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
you should override sourceSets like this
sourceSets {
main.java.srcDirs = []
main.kotlin.srcDirs = ['src/main/java', 'src/main/kotlin']
main.resources.srcDirs = ['src/main/resources']
}
I had a similar issue and my java code was in the same source directory as the kotlin code
My solution was adding this configuration in build.gradle.kts:
configure<SourceSetContainer> {
named("main") {
java.srcDir("src/main/kotlin")
}
}
If anyone stumbles across this thread using the Android Gradle plugin;
android {
sourceSets {
named("main") {
java.srcDir("src/main/kotlin")
}
}
}

Multiple Repositories in build.gradle is not working (jzy3d) (MultiModuleProject)

I've got a problem with my Java project in gradle. I'm using several dependencies with gradle, one of it is the jzy3d.api. I have included it in the build.gradle file and added the url to the repositories but it doesn't find it. I've added the repository for it as it is located here: http://maven.jzy3d.org/releases/org/jzy3d/jzy3d-api/1.0.2/
But the Error Message seems like that it isn't searching there. Can please someone tell me why?
Here is my build.gradle and the Error Message:
plugins {
id 'java'
id 'idea' // optional, for IntelliJ IDEA project
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
maven {
url "https://maven.jzy3d.org/releases"
}
mavenCentral()
}
dependencies {
testCompile 'org.junit.jupiter:junit-jupiter:5.6.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
compile group: 'org.jzy3d', name: 'jzy3d-api', version: '1.0.2'
compile group: 'org.jfree', name: 'jfreechart', version: '1.5.0'
}
Execution failed for task ':BusinessLayer:compileJava'.
> Could not resolve all files for configuration ':BusinessLayer:compileClasspath'.
> Could not find org.jzy3d:jzy3d-api:1.0.2.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/org/jzy3d/jzy3d-api/1.0.2/jzy3d-api-1.0.2.pom
If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
Required by:
project :BusinessLayer > project :DataLayer
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
When I'm adding something to the url, for example url "https://maven.jzy3d.org/releases/maven2" the Error Message says:
Could not find org.jzy3d:jzy3d-api:1.0.2.
Searched in the following locations:
- https://maven.jzy3d.org/releases/maven2/org/jzy3d/jzy3d-api/1.0.2/jzy3d-api-1.0.2.pom
- https://repo.maven.apache.org/maven2/org/jzy3d/jzy3d-api/1.0.2/jzy3d-api-1.0.2.pom
So I think that for some reason it don't want to search in the https://maven.jzy3d.org/releases directory...

Deeplearning4j example does not work with gradle

I am trying to run CnnSentenceClassification from deeplearning4j example. I moved this file to my Gradle project. When I run the class from the eclipse it works fine. However when I run it from ./gradlew run I get following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at
main.CnnSentenceClassification.main(CnnSentenceClassification.java:75)
Caused by: java.lang.RuntimeException:
org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException:
Please ensure that you have an nd4j backend on your classpath. Please
see: http://nd4j.org/getstarted.html
at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6089)
at org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:201)
... 1 more
Caused by:
org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException:
Please ensure that you have an nd4j backend on your classpath. Please
see: http://nd4j.org/getstarted.html
at org.nd4j.linalg.factory.Nd4jBackend.load(Nd4jBackend.java:258)
at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6086)
... 2 more
I checked and nd4j-api-0.9.1.jar is in my classpath. This is my build.gradle:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
repositories {
jcenter()
}
mainClassName="main.CnnSentenceClassification"
dependencies {
compile group: 'org.deeplearning4j', name: 'deeplearning4j-core', version: '0.9.1'
compile group: 'org.deeplearning4j', name: 'deeplearning4j-nlp', version: '0.9.1'
testCompile group: 'org.nd4j', name: 'nd4j-native-platform', version: '0.9.1'
compile group: 'org.nd4j', name: 'nd4j-api', version: '0.9.1'
compile "org.slf4j:slf4j-simple:1.7.25"
compile "org.slf4j:slf4j-api:1.7.25"
}
I was having the same problem. You need an ND4J backend, which means updating your dependency tree.
For Maven builds, add the following dependency to your project's pom:
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>0.9.1</version>
</dependency>
For Gradle builds, just add the following line in your dependencies:
compile "org.nd4j:nd4j-native:0.9.1"
This native backend uses the CPU for computations.
There is another dependency for a CUDA-enabled graphics card.
I found this link helpful:
DL4J Performance Debugging
Edit: new link is here. https://deeplearning4j.konduit.ai/config/backends
Old content below:
You have test scope on the nd4j backend. An nd4j backend is NEVER optional.
https://nd4j.org/backend.html
The error is right in the message. We even give you a link with an explanation right in the stack trace.

which dependency will be downloaded?

I have a build script:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group : 'org.hibernate', name : 'hibernate-core', version :'4.+'
testCompile group : 'junit', name : 'junit', version: '4.+'
}
My question is what exactly version will be downloaded? The latest available version or random? What if some version, satisfies 4.+ restricon are incompatible?
This is about downloading, not uploading. 4.+ will resolve to the latest 4.x version. Not sure I understand your other question about compatibility (you'd have to explain in more detail).

Categories