Run As JUnit - ClassNotFoundException - BuildCommand Fix causes class resolution problems - java

Here's my Setup:
Eclipse Oxygen
Building and Deploying with Maven 3.3 which includes these specific dependencies for Unit Testing:
JUnit 4.4
Mockito 1.9.5
I've created a JUnit test class and added it to ($project/src/test/java).
I added this folder as a source folder and then did a Run As JUnit.
I recieve the following error:
Class not found
java.lang.ClassNotFoundException:
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:499)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
I can fix this by adding the following to the .project file Build Spec:
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
Unfortunately this seems to cause multiple errors to show up under markers for Java Server Faces, etc.
Is this really my fix or could I be missing something else? It's especialloy frustrating when you have a clean workspace before making this change to fix the "Junit" issue.

These extra dependencies need to be added to the pom.xml
<!-- JSF(MyFaces & Tomahawk) Dependencies -->
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>1.1.5</version>
<exclusions>
<exclusion>
<artifactId>javax.servlet</artifactId>
<groupId>jstl</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-impl</artifactId>
<version>1.1.5</version>
<exclusions>
<exclusion>
<artifactId>javax.servlet</artifactId>
<groupId>jstl</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.myfaces.tomahawk</groupId>
<artifactId>tomahawk</artifactId>
<version>1.1.6</version>
<exclusions>
<exclusion>
<artifactId>javax.servlet</artifactId>
<groupId>jstl</groupId>
</exclusion>
</exclusions>
</dependency>
The actual jar files can be added as an external library as well, but that would need to be done each time you create a project.

Related

ClassNotFoundException: io.opencensus.trace.propagation.TextFormat not found

I have a module in apache karaf 4.2.6 with java 11 that validates purchase receipts in Google Play. I'm using androidpublisher.
When it sends a request for the first time I get an error in OpenCensusUtils class:
androidPublisher.purchases().products().get(packageName, productId, purchaseToken).execute()
java.lang.ClassNotFoundException: io.opencensus.trace.propagation.TextFormat not found by wrap_file__Users_USER_NAME_.m2_repository_io_opencensus_opencensus-contrib-http-util_0.22.1_opencensus-contrib-http-util-0.22.1.jar
In subsequent times I get error in com.google.api.client.http.HttpRequest class at this point private final Tracer tracer = OpenCensusUtils.getTracer():
java.lang.NoClassDefFoundError: Could not initialize class com.google.api.client.http.OpenCensusUtils
Here is dependencies:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-androidpublisher</artifactId>
<version>v3-rev92-1.25.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.29.2</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>0.22.1</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-http-util</artifactId>
<version>0.22.1</version>
</dependency>
Also google-api-client, google-http-client-jackson2 and google-oauth-client has version 1.29.2
In karaf I see the library opencensus-api is exporting a package io.opencensus.trace.propagation that contains a class TextFormat.
What am I doing wrong?
I resolved by adding io.opencensus and io.grpc to the Private-Package of the module's build area as follows. It made them embed locally.
....
<build>
<plugins>
<plugin>
....
<configuration>
<instructions>
<Bundle-SymbolicName> ${project.artifactId} </Bundle-SymbolicName>
<Private-Package>
META-INF.services;-split-package:=merge-first,
com.google.*;-split-package:=merge-first,
io.opencensus.*;-split-package:=merge-first,
io.grpc.*;-split-package:=merge-first
</Private-Package>
....

java.lang.NoClassDefFoundError: Failed to link org/pentaho/di/core/attributes/metastore/EmbeddedMetaStore

I'm building a batch with Pentaho Kettle to process some files. This batch is started by a servlet. However, when I try to start it, I get the following warn which actually block the process:
WARN [org.jboss.modules] (Batch Thread - 2) Failed to define class org.pentaho.di.core.attributes.metastore.EmbeddedMetaStore in Module "deployment.my-program-1.0.0.0-SNAPSHOT.war:main" from Service Module Loader: java.lang.NoClassDefFoundError: Failed to link org/pentaho/di/core/attributes/metastore/EmbeddedMetaStore (Module "deployment.my-program-1.0.0.0-SNAPSHOT.war:main" from Service Module Loader): org/pentaho/metastore/api/BaseMetaStore
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:446)
at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:274)
at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:78)
at org.jboss.modules.Module.loadModuleClass(Module.java:605)
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
at org.pentaho.di.base.AbstractMeta.<init>(AbstractMeta.java:162)
at org.pentaho.di.job.JobMeta.<init>(JobMeta.java:201)
at org.pentaho.di.job.JobMeta.<init>(JobMeta.java:755)
However, when I look in my lib folder, the jar containing this class (kettle-engine-8.2.0.0-342.jar) is there and the class is also in it.
I'm using a JBoss EAP 7.0 with Java 1.8.0.161 on my computer for development. I start it with my IDE (Eclipse Photon). Here are my dependencies in the pom.xml:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- vf2 debug -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.2</version>
</dependency>
<!-- maven-war-plugin-->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</dependency>
<!-- pentaho kettle -->
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
<version>8.2.0.0-342</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
<version>8.2.0.0-342</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>2.5.16</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7R3</version>
</dependency>
<!-- this correct an error -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- netty version override to correct netty 3.7 bug / FYI, pentaho don't use netty anyway-->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.9.0.Final</version>
</dependency>
</dependencies>
Important : this issue does not occur when using previous version of kettle-core and kettle-engine (tested with 7.0).
It's an embedded call so I do not directly call this class in my code.
I do not have multiple versions of this jar in my classpath.
Another person in my company found out so i'll just answer here to close the topic.
I didn't show it here but in my dependencies I had a dependency to some custom framework from work. This framework actually included pentaho, but the 7.0 version. Then, #suresh was right when he talk about multiple version jar.
So the answer is :
Check if any dependency actually contains duplicate dependency you're calling somewhere else. To avoid this issue, use the excluding syntax in the dependency calling the wrong versions :
<exclusions>
<exclusion>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
</exclusion>
<exclusion>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
</exclusion>
</exclusions>

Maven builds are working, but junit is throwing a NoSuchMethodError

Just added PowerMock to my projects pom files so I can start mocking some static methods. I've verified all the versions are up to date, this includes Juint, Javassit, and mockito. The one line that is causing this problem is this:
#RunWith(PowerMockRunner.class)
When that line is commented out, my code runs fine through Junit, with it Junit has a heart attack, but a maven build works perfectly. Now I can get used to working with maven builds, but I would love the ability to debug my tests through Junit.
I have tried the following: cleaning eclipse, using maven installs/cleans/builds from both Terminal and Eclipse's UI, furiously pressing F5 while rocking myself in a dark corner for 2 hours. Any assistance on this problem would be greatly appreciated.
Below is the failure trace when I try to run as a junit test.
java.lang.NoSuchMethodError: javassist.CtMethod.hasAnnotation(Ljava/lang/Class;)Z
at org.powermock.core.transformers.impl.TestClassTransformer.removeTestAnnotationsForTestMethodsThatRunOnOtherClassLoader(TestClassTransformer.java:185)
at org.powermock.core.transformers.impl.TestClassTransformer.transform(TestClassTransformer.java:198)
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:251)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:180)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:145)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:40)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:244)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:61)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock</artifactId>
<version>1.6.2</version>
<scope>test</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.4.GA</version>
<scope>test</scope>
</dependency>
The information you provided is not enough to find out what could be the problem. I would recommend you to run
mvn dependency:tree
It will list you all the dependencies, that you have in your project.Try to find simillar dependencies with different versions.
Another possible solution: Do you have the correct Javassist version (3.18.2) in the classpath?
So I'm using Raptor (I believe that's an in house version of Eclipse) which forces certain dependency version installs (even without a pom). Even though I only had the right versions in my pom files, Raptor was installing incompatible versions before hand, which took precedence. (at least thats what I believe was happening)
I had to do some forceful version control in my parent pom using dependency managers, and I seemed to have cleared up the issue. Though a few more problems followed with the libraries not having the correct methods. I ended up resolving those issues by finding versions of the dependencies (allowed by companies repository manager) that worked together (a lot of tinkering and seeing which versions would play nice). Because of the repository manager I was unable to download just the latest version of each dependency I needed.
If you are experiencing this problem make sure your versions are compatible, if you can get the newest version of the dependencies do that. If not, grab a beer, and start with the latest versions offered by your repository manager and start trying to find versions that work with each other.
I found the fix. I had to add
<dependency>
<groupId>com.ebay.raptor.core</groupId>
<artifactId>RaptorKernel</artifactId>
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>javassist</groupId>
</exclusion>
</exclusions>
</dependency>
RaptorKernel could be replaced by any artifact which is bringing in the javassist different(other than 3.18.2) dependency.

[JAVA Spring SOAP WSDL]

I try to connect to a Web Service using spring/soap/wsdl. I'm receiving a stack trace like this:
Exception in thread "main" org.springframework.ws.soap.SoapMessageCreationException: Could not create message from InputStream: Unable to create envelope from given source: ; nested exception is com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source:
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:216)
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:60)
at org.springframework.ws.transport.AbstractWebServiceConnection.receive(AbstractWebServiceConnection.java:92)
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:608)
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390)
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:383)
at com.arek.soapallegrotest.WsSearchClient.doLogin(WsSearchClient.java:60)
at com.arek.soapallegrotest.WsSearchClient.doSearchResponse(WsSearchClient.java:81)
at com.arek.soapallegrotest.SpringMain.main(SpringMain.java:24)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source:
at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:117)
at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:69)
at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:128)
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:189)
... 9 more
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source because the root element is not named "Envelope"
at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForEnvelope(SOAPPartImpl.java:154)
at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:121)
at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(Envel
opeFactory.java:110)
... 12 more
2015-03-30 16:30:26.747 INFO 12079 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#3cb1ffe6: startup date [Mon Mar 30 16:30:24 CEST 2015]; root of context hierarchy
I've used this tutorial: https://spring.io/guides/gs/consuming-web-service/#initial to learn my self.
I found some info that I have something wrong with namespace ?
ps. sorry for my english
I had same problem and the cause was in line like this:
WebServiceTemplate template = new WebServiceTemplate(messageFactory());
...
template.setDefaultUri("http://host:2121/portal/service?wsdl");
solution was to remove ?wsdl
I had the same problem. I don't know exactly what is causing this in your sources because you didn't add any specific informatcion, but I can tell it began giving this error in my project when I added fop to my pom:
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.1</version>
<!-- some exclusions needed because of inexistent "avalon" packages in maven repositories -->
</dependency>
It did happen with every version of fop I could find through maven.
I found a solution here. Apparently, the xalan and xerces included in fop needed to be upgraded.
I'm not happy with this solution, but it works. I left some exclusions I'm not sure are needed or could damage something else because a deadline is coming very fast (ha ha ha). I'll get into it later (or hope someone else can comment about them).
Here I add my pom fragment:
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.1</version>
<exclusions>
<!--
Following avalon-framework versions that come
as dependencies for fop 1.1 are not found
through maven:
See: https://issues.apache.org/jira/browse/FOP-2151
-->
<exclusion>
<groupId>org.apache.avalon.framework</groupId>
<artifactId>avalon-framework-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.avalon.framework</groupId>
<artifactId>avalon-framework-impl</artifactId>
</exclusion>
<!--
Without following exclusions I get the following
exception when trying to access another web service:
2015-04-24 13:48:25,048 ERROR [http-nio-8084-exec-2] (CfdiController.java:152) -
Error while uploading. org.springframework.ws.soap.SoapMessageCreationException:
Could not create message from InputStream: Unable to create envelope from given
source: ; nested exception is com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:
Unable to create envelope from given source:
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:216)
~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
See: https://stackoverflow.com/a/13156775/2796922
-->
<exclusion>
<artifactId>maven-cobertura-plugin</artifactId>
<groupId>maven-plugins</groupId>
</exclusion>
<exclusion>
<artifactId>maven-findbugs-plugin</artifactId>
<groupId>maven-plugins</groupId>
</exclusion>
<exclusion>
<artifactId>xalan</artifactId>
<groupId>xalan</groupId>
</exclusion>
<exclusion>
<artifactId>xercesImpl</artifactId>
<groupId>xerces</groupId>
</exclusion>
</exclusions>
</dependency>
<!--
These avalon dependencies are found in maven:
-->
<dependency>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework-api</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework-impl</artifactId>
<version>4.2.0</version>
</dependency>
<!--
With these versions is no longer raised the
"Unable to create envelope from given source"
exception explained before
-->
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.0</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
</exclusions>
</dependency>
Hope it helps.

NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()

The issue was caused by one of the dependencies in my pom.xml [cxf-bundle-jaxrs] which internally uses lower version of slf4j. I managed to resolve this issue by upgrading this dependency to the latest release. Thanks everyone.
I'm trying to add Apache Shiro to my CXF Spring web application. When I start up my tomcat 7 I get the following error
Caused by: java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:121)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:268)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:241)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:254)
at org.apache.shiro.spring.LifecycleBeanPostProcessor.<clinit>(LifecycleBeanPostProcessor.java:51)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
... 25 more
and my pom.xml for shiro and slf4j is
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>
I have tried every possible solution by googling, but no luck.
Caused by: java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;
This means that you have the class StaticLoggerBinder in your classpath.
But the class of your classpath does not have the method getSingleton()
This usually happens when you have dependencies that both use the same transitive dependency. This means that 2 of your dependencies (or your app, and a transitive dependency) are both using SLF4J internally with different versions. But your app can only use a single version of the class at the same time so it has to choose (don't know the rules here... random?)
To solve the problem, you usually need to do a mvn dependency:tree to see which are using different versions of SLF4J.
The solution is often to use a maven dependency exclusion on the lowest version. Libraries like SLF4J tend to be retrocompatible which means newer versions keep adding more features. Sometimes, a method is removed and then you have to keep the old library version, and pray. If this doesn't work there are still some options, like JarJar
But for logging libraries it's sometimes a bit different, so my explaination is for general purpose, but it's probable you do not use the right logging library dependencies because they have very special packagings which are not always easy to understand :)
Check what I found for you:
https://github.com/qos-ch/slf4j/blob/SLF4J_1.5.5/slf4j-api/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
https://github.com/qos-ch/slf4j/blob/SLF4J_1.5.6/slf4j-api/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
The getSingleton() method appeared at version 1.5.6, so it is very probable that you have a library that uses an SLF4J version older that 1.5.6 :) You just need to exclude it with a maven exclusion (and pray).
Edit: you should try:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
Better, if you have a multimodule maven project with a parent pom you can use this with maven dependencyManagement xml node.
You need to include this dependency:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
Something seems to be not compatible with those sl4j-api and log4j versions.
Try this link and find the correct and compatible dependencies and use them.
May be you should try the following combination,
SLF4J API Module 1.7.5 The slf4j API
SLF4J LOG4J-12 Binding 1.7.5 SLF4J LOG4J-12 Binding
Or, if you are planning to use version 1.6.1
<!-- slf4j-log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
Adding this dependency solved the issue for me
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>

Categories