I'm trying to connect to HiveServer2 and run queries from my java application. When I run on my localserver(Hive version: 2.1.1). It works perfectly with the following dependencies:
compile('org.apache.hadoop:hadoop-core:1.2.1')
compile ('org.apache.hive:hive-jdbc:2.1.0'){
exclude group: 'org.eclipse.jetty.aggregate', module: '*'
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j" }
compile ('org.apache.hadoop:hadoop-common:3.0.0'){
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}
But when I run on a remote server(Hive version: 0.13.1-SNAPSHOT). It throws the below mentioned error. I got to know that, the issue is with hive-jdbc, hadoop-core and hadoop-common versions. Can anyone let me know which versions of those dependencies I need to be using for Hive 0.13.1-SNAPSHOT
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]
After a few trail and errors I fixed the version issue. The issue is hive-jdbc:2.1.0 is not backward compatible with Hive 0.13.1-SNAPSHOT, we need to use hive-jdbc:0.13.1 to make it work. Below code works with no doubt.
compile('org.apache.hadoop:hadoop-core:1.2.1')
compile ('org.apache.hive:hive-jdbc:0.13.1'){
exclude group: 'org.eclipse.jetty.aggregate', module: '*'
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j" }
compile ('org.apache.hadoop:hadoop-common:3.0.0'){
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}
Related
I have a Java project that builds a war. junit-4.13.2.jar is being included in the war as a transitive dependency. I'd like to exclude the jar from the war, but I still need to have the jar for running tests.
My build.gradle looks like:
dependencies {
...
testCompile group: 'junit', name: 'junit'
...
}
configurations.all {
exclude group: "org.apache.logging.log4j", module: "log4j-core"
}
Using this config, I can still run my tests, but the jar still ends up in the war.
As you already state that its because of transitive dependency from a compile/runtime dependency - the fix would be to exclude junit from the same
implementation ('g:a:v') {
exclude group: 'junit', module: 'junit'
}
I upgraded the plugin id 'org.springframework.boot' version '2.6.5' which led to the following changes
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'jacoco'
id 'org.barfuin.gradle.jacocolog' version '1.0.1'
}
bootRun {
args = [
'--spring.config.location=./src/configs.properties',
]
}
springBoot {
mainClass = 'com.company.service.ServiceClass'
}
jar {
// This would avoid creating an additional "-plain" jar
enabled = true
archiveClassifier = ''
// Before the plugin upgrade I wasnt getting manifest attribute related error. Because of one of such errors, I added it the attribute explicitly.
manifest {
attributes 'Main-Class': 'com.company.service.ServiceClass'
}
}
bootJar {
layered {
enabled = true
}
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'org.bouncycastle:bcprov-jdk16:1.46'
implementation 'org.immutables:value:2.9.0'
implementation('org.apache.lucene:lucene-core:8.10.0')
implementation('com.fasterxml.jackson.core:jackson-core:2.13.2')
implementation('com.fasterxml.jackson.core:jackson-annotations:2.13.2')
implementation('com.fasterxml.jackson.core:jackson-databind:2.13.2')
implementation('org.apache.poi:poi-ooxml:5.2.2')
implementation('io.netty:netty-codec:4.1.75.Final')
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.13.2'
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.immutables:value:2.9.0'
implementation('org.springframework.boot:spring-boot-starter-web:2.6.5') {
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
implementation('org.apache.tomcat.embed:tomcat-embed-core:9.0.54')
}
implementation('org.springframework.boot:spring-boot-starter-security:2.6.5') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
implementation('org.keycloak:keycloak-spring-boot-starter:17.0.1') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
implementation('org.keycloak.bom:keycloak-adapter-bom:17.0.1')
implementation('io.springfox:springfox-boot-starter:3.0.0')
implementation('io.springfox:springfox-swagger-ui:3.0.0')
}
// some jacoco and checkstyle configs here.
When I run the jar, I get the following error
>> java -jar build/libs/service-1.0-SNAPSHOT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at java.lang.Class.getDeclaredMethods0(Native Method)
From the solutions I found on SO, I added the dependency
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
But this doesnt work. Plus I am still using jdk8 and there is no org.bouncycastle for jdk8.
Any suggestions how I can work around this exception?
EDIT:
Running the application with intelliJ doesnt throw this problem. But happens when running from command line.
EDIT:
>> % java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
Full stack trace
% java -jar build/libs/my-service-class.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
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:650)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 7 more
Running the application with intelliJ doesnt throw this problem. But
happens when running from command line.
java -jar build/libs/my-service-class.jar
For executing a jar directly from command line, it should contain manifest information. Assuming the aforementioned jar contain the same as you're facing the the other issue(class def not found) so looks like your jar my-service-class.jar is thin jar and doesn't contain the other dependencies that you've added in the project. Two way to solve this problem. Either you can create uber jar i.e jar with all the dependencies included by using available plugins for the same and then execute the command or just add all the required dependencies into some folder and give path to it while runing the above command.
I am getting the following error when I build my project, I tried various other StackOverflow answers but all in vain.
Also tried to exclude logback from all the dependencies,
configurations {
implementation {
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
}
And the above does solve the problem but gives another error java.lang.NoClassDefFoundError: ch/qos/logback/classic/turbo/TurboFilter, seems like it is using logback some place else and hence logback cannot be excluded from all the dependencies. Any suggestion on how to get rid of the issue would be really valuable. Thanks.
Error log
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a
Logback LoggerContext but Logback is on the classpath. Either remove
Logback or the competing implementation (class
org.jboss.slf4j.JBossLoggerFactory loaded from
file:/C:/Users/idnpga/.gradle/caches/modules-2/files-2.1/org.infinispan/infinispan-embedded/9.1.7.Final/6b94fd0e5872b8ce90ab35531afe90d1efaf36f0/infinispan-embedded-9.1.7.Final.jar).
If you are using WebLogic you will need to add 'org.slf4j' to
prefer-application-packages in WEB-INF/weblogic.xml:
org.jboss.slf4j.JBossLoggerFactory at
org.springframework.util.Assert.instanceCheckFailed(Assert.java:696)
at org.springframework.util.Assert.isInstanceOf(Assert.java:596) at
org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:284)
at
org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:104)
at
org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:232)
at
org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:213)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at
org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:74)
at
org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:47)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
at
org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
... 81 more
build.gradle
dependencies {
implementation('org.springframework.boot:spring-boot-starter-actuator')
implementation('org.springframework.boot:spring-boot-starter-amqp')
implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
implementation('org.springframework.boot:spring-boot-starter-data-rest')
implementation('org.springframework.boot:spring-boot-starter-integration')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.boot:spring-boot-starter-thymeleaf') {
exclude group: 'org.codehaus.groovy', module: 'groovy'
}
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-web-services')
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version: '2.0.5.RELEASE'
implementation('org.springframework.data:spring-data-rest-hal-explorer')
implementation('org.springframework.integration:spring-integration-amqp')
implementation('org.springframework.integration:spring-integration-mongodb')
implementation('org.springframework.integration:spring-integration-file')
implementation('org.springframework.integration:spring-integration-http')
implementation('org.springframework.integration:spring-integration-ip')
implementation('org.springframework.integration:spring-integration-sftp')
implementation('org.springframework.integration:spring-integration-stream')
implementation('org.springframework.integration:spring-integration-xml')
implementation('org.springframework.ws:spring-ws-security') {
exclude group: 'org.opensaml:opensaml-saml-impl'
}
implementation ('org.springframework.security:spring-security-oauth2-core') {
exclude group: 'org.codehaus.jackson'
}
implementation group: 'org.springframework.data', name: 'spring-data-mongodb', version: '3.0.4.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix', version: '2.1.5.RELEASE'
implementation 'org.infinispan:infinispan-spring-boot-starter-parent:14.0.0.Dev01'
implementation 'org.infinispan:spring-boot-starter-infinispan-autoconfigure:1.0.0'
implementation('org.infinispan:infinispan-embedded:9.1.7.Final')
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.9'
testImplementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.9'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.9'
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.9'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module : 'junit-vintage-engine'
}
testImplementation('org.springframework.ws:spring-ws-test')
testImplementation("org.springframework.cloud:spring-cloud-contract-wiremock:3.1.1")
testImplementation("org.springframework.cloud:spring-cloud-starter-contract-verifier") {
exclude group: 'org.springframework.boot', module : 'spring-boot-starter-test'
}
}
Unable to start spring-boot in jetty even with the following configuration, what is not being configured properly
implementation ('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
You need to add below code in your build.gradle to exclude Tomcat.
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
and then to add Jetty dependecy:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
And this will start your Spring Boot app with Jetty.
I'm trying to run Java tests with PowerMock version 1.7.4 and Mockito version 1.10.19, using Gradle.
PowerMock 1.7.4 has transitive dependencies on both
org.mockito » mockito-core 1.10.19
and on
org.mockito » mockito-core 2.8.9
(See https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito-common/1.7.4)
In order to NOT bring in Mockito 2.8.9, but instead have tests compile and run against Mockito 1.10.19, I have to do the following (because of various transitive dependencies within the powermock jars):
testCompile ("org.mockito:mockito-core:${versions.mockito}") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
testCompile ("org.powermock:powermock-api-mockito-common:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
testCompile ("org.powermock:powermock-api-mockito:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
testCompile ("org.powermock:powermock-core:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
testCompile ("org.powermock:powermock-module-junit4:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-module-junit4-common'
}
testCompile ("org.powermock:powermock-module-junit4-common:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testCompile ("org.powermock:powermock-api-support:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testCompile ("org.powermock:powermock-api-easymock:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-api-support'
}
testCompile ("org.powermock:powermock-module-javaagent:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testCompile ("org.powermock:powermock-module-junit4-rule-agent:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testRuntime("org.mockito:mockito-core:${versions.mockito}") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
Where versions is:
versions = [
'mockito': '1.10.19',
'powermock': '1.7.4'
]
Surely there is a better, more concise way of doing this in Gradle, right?
One way to compact it, excluding only mockito-core :
testCompile ("org.mockito:mockito-core:${versions.mockito}") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
['powermock-api-mockito-common',
'powermock-api-mockito',
'powermock-core',
'powermock-module-junit4',
'powermock-module-junit4-common',
'powermock-api-support',
'powermock-api-easymock',
'powermock-module-javaagent',
'powermock-module-junit4-rule-agent'].each {
testCompile ("org.powermock:${it}:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
}
Moreover the testRuntime mockito-core is useless as testRuntime extends testCompile