macha
macha

Reputation: 7497

SLF4J: Exception in thread "main" java.lang.NoSuchMethodError

I am trying to work on an intro project with HBase but I am running into an error with SLF4J. I do not even use SLF4J in my project, but I am unable to understand how to fix this issue. I have gone through a few other posts here and found that excluding SLF4J from other imports should be performed to resolve this issue, but it looks like I am still running into this issue. I am adding the error, pom.xml, dependency tree and the java command that I am running.

Error

log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding.
SLF4J: Your binding is version 1.5.5 or earlier.
SLF4J: Upgrade your binding to version 1.6.x.
Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:107)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:295)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
    at org.apache.zookeeper.ZooKeeper.<clinit>(ZooKeeper.java:94)
    at org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper.<init>(RecoverableZooKeeper.java:98)
    at org.apache.hadoop.hbase.zookeeper.ZKUtil.connect(ZKUtil.java:127)
    at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.<init>(ZooKeeperWatcher.java:153)
    at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.<init>(ZooKeeperWatcher.java:127)
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getZooKeeperWatcher(HConnectionManager.java:1505)
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.ensureZookeeperTrackers(HConnectionManager.java:713)
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:983)
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:958)
    at org.apache.hadoop.hbase.client.HTable.finishSetup(HTable.java:251)
    at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:243)
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getTable(HConnectionManager.java:668)
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getTable(HConnectionManager.java:655)
    at com.hbase_intro.HBaseIntro.AccessObject.addUser(AccessObject.java:103)
    at com.hbase_intro.HBaseIntro.HelloWorld.main(HelloWorld.java:32)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hbase-intro</groupId>
  <artifactId>HBaseIntro</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>HBaseIntro</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.4.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase</artifactId>
        <version>0.94.18</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.3</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.hbase_intro.HBaseIntro.HelloWorld</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

Dependency Tree (I generated the dependency tree with verbose flag, and it looks like SLF4J 1.4.3 is being compiled

+- junit:junit:jar:3.8.1:test
+- org.apache.hadoop:hadoop-common:jar:2.4.0:compile
|  +- org.apache.hadoop:hadoop-annotations:jar:2.4.0:compile
|  |  \- jdk.tools:jdk.tools:jar:1.7:system
|  +- com.google.guava:guava:jar:11.0.2:compile
|  |  \- (com.google.code.findbugs:jsr305:jar:1.3.9:compile - omitted for duplicate)
|  +- commons-cli:commons-cli:jar:1.2:compile
|  +- org.apache.commons:commons-math3:jar:3.1.1:compile
|  +- xmlenc:xmlenc:jar:0.52:compile
|  +- commons-httpclient:commons-httpclient:jar:3.1:compile
|  |  +- (commons-logging:commons-logging:jar:1.0.4:compile - omitted for conflict with 1.1.3)
|  |  \- (commons-codec:commons-codec:jar:1.2:compile - omitted for conflict with 1.4)
|  +- commons-codec:commons-codec:jar:1.4:compile
|  +- commons-io:commons-io:jar:2.4:compile
|  +- commons-net:commons-net:jar:3.1:compile
|  +- commons-collections:commons-collections:jar:3.2.1:compile
|  +- javax.servlet:servlet-api:jar:2.5:compile
|  +- org.mortbay.jetty:jetty:jar:6.1.26:compile
|  |  \- (org.mortbay.jetty:jetty-util:jar:6.1.26:compile - omitted for duplicate)
|  +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
|  +- com.sun.jersey:jersey-core:jar:1.9:compile
|  +- com.sun.jersey:jersey-json:jar:1.9:compile
|  |  +- org.codehaus.jettison:jettison:jar:1.1:compile
|  |  +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:compile
|  |  |  \- (javax.xml.bind:jaxb-api:jar:2.2.2:compile - omitted for conflict with 2.1)
|  |  +- (org.codehaus.jackson:jackson-core-asl:jar:1.8.3:compile - omitted for conflict with 1.8.8)
|  |  +- (org.codehaus.jackson:jackson-mapper-asl:jar:1.8.3:compile - omitted for conflict with 1.8.8)
|  |  +- (org.codehaus.jackson:jackson-jaxrs:jar:1.8.3:compile - omitted for conflict with 1.8.8)
|  |  +- (org.codehaus.jackson:jackson-xc:jar:1.8.3:compile - omitted for conflict with 1.8.8)
|  |  \- (com.sun.jersey:jersey-core:jar:1.9:compile - omitted for duplicate)
|  +- com.sun.jersey:jersey-server:jar:1.9:compile
|  |  +- asm:asm:jar:3.1:compile
|  |  \- (com.sun.jersey:jersey-core:jar:1.9:compile - omitted for duplicate)
|  +- tomcat:jasper-compiler:jar:5.5.23:runtime
|  +- tomcat:jasper-runtime:jar:5.5.23:runtime
|  |  +- (javax.servlet:servlet-api:jar:2.4:runtime - omitted for conflict with 2.5)
|  |  \- (commons-el:commons-el:jar:1.0:runtime - omitted for duplicate)
|  +- javax.servlet.jsp:jsp-api:jar:2.1:runtime
|  +- commons-el:commons-el:jar:1.0:runtime
|  |  \- (commons-logging:commons-logging:jar:1.0.3:runtime - omitted for conflict with 1.0.4)
|  +- commons-logging:commons-logging:jar:1.1.3:compile
|  +- log4j:log4j:jar:1.2.17:compile
|  +- net.java.dev.jets3t:jets3t:jar:0.9.0:compile
|  |  +- (commons-codec:commons-codec:jar:1.4:compile - omitted for duplicate)
|  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.1.3)
|  |  +- org.apache.httpcomponents:httpclient:jar:4.1.2:compile
|  |  |  \- (org.apache.httpcomponents:httpcore:jar:4.1.2:compile - omitted for duplicate)
|  |  +- org.apache.httpcomponents:httpcore:jar:4.1.2:compile
|  |  \- com.jamesmurty.utils:java-xmlbuilder:jar:0.4:compile
|  +- commons-lang:commons-lang:jar:2.6:compile
|  +- commons-configuration:commons-configuration:jar:1.6:compile
|  |  +- (commons-collections:commons-collections:jar:3.2.1:compile - omitted for duplicate)
|  |  +- (commons-lang:commons-lang:jar:2.4:compile - omitted for conflict with 2.6)
|  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.1.3)
|  |  +- commons-digester:commons-digester:jar:1.8:compile
|  |  |  +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
|  |  |  |  \- (commons-logging:commons-logging:jar:1.0.3:compile - omitted for conflict with 1.1.3)
|  |  |  \- (commons-logging:commons-logging:jar:1.1:compile - omitted for conflict with 1.1.3)
|  |  \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
|  |     \- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.1.3)
|  +- (org.slf4j:slf4j-log4j12:jar:1.7.5:compile - scope updated from runtime; omitted for duplicate)
|  +- org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile
|  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.8.8:compile
|  |  \- (org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile - omitted for duplicate)
|  +- org.apache.avro:avro:jar:1.7.4:compile
|  |  +- (org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile - omitted for duplicate)
|  |  +- (org.codehaus.jackson:jackson-mapper-asl:jar:1.8.8:compile - omitted for duplicate)
|  |  +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
|  |  +- org.xerial.snappy:snappy-java:jar:1.0.4.1:compile
|  |  \- (org.apache.commons:commons-compress:jar:1.4.1:compile - omitted for duplicate)
|  +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
|  +- org.apache.hadoop:hadoop-auth:jar:2.4.0:compile
|  |  +- (commons-codec:commons-codec:jar:1.4:compile - omitted for duplicate)
|  |  +- (log4j:log4j:jar:1.2.17:runtime - omitted for duplicate)
|  |  +- (org.slf4j:slf4j-log4j12:jar:1.7.5:runtime - omitted for duplicate)
|  |  \- (org.apache.httpcomponents:httpclient:jar:4.2.5:compile - omitted for conflict with 4.1.2)
|  +- com.jcraft:jsch:jar:0.1.42:compile
|  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
|  +- org.apache.zookeeper:zookeeper:jar:3.4.5:compile
|  |  +- (org.slf4j:slf4j-log4j12:jar:1.7.5:compile - omitted for conflict with 1.4.3)
|  |  \- (log4j:log4j:jar:1.2.15:compile - omitted for conflict with 1.2.17)
|  \- org.apache.commons:commons-compress:jar:1.4.1:compile
|     \- org.tukaani:xz:jar:1.0:compile
+- org.apache.hbase:hbase:jar:0.94.18:compile
|  +- com.yammer.metrics:metrics-core:jar:2.1.2:compile
|  +- (com.google.guava:guava:jar:11.0.2:compile - omitted for duplicate)
|  +- (commons-cli:commons-cli:jar:1.2:compile - omitted for duplicate)
|  +- (commons-configuration:commons-configuration:jar:1.6:compile - omitted for duplicate)
|  +- com.github.stephenc.high-scale-lib:high-scale-lib:jar:1.1.1:compile
|  +- (commons-codec:commons-codec:jar:1.4:compile - omitted for duplicate)
|  +- (commons-httpclient:commons-httpclient:jar:3.1:compile - omitted for duplicate)
|  +- (commons-io:commons-io:jar:2.1:compile - omitted for conflict with 2.4)
|  +- (commons-lang:commons-lang:jar:2.5:compile - omitted for conflict with 2.6)
|  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.1.3)
|  +- (log4j:log4j:jar:1.2.16:compile - omitted for conflict with 1.2.17)
|  +- (org.apache.avro:avro:jar:1.5.3:compile - omitted for conflict with 1.7.4)
|  +- org.apache.avro:avro-ipc:jar:1.5.3:compile
|  |  +- (org.apache.avro:avro:jar:1.5.3:compile - omitted for conflict with 1.7.4)
|  |  +- (org.mortbay.jetty:jetty:jar:6.1.26:compile - omitted for duplicate)
|  |  +- org.jboss.netty:netty:jar:3.2.4.Final:compile
|  |  \- org.apache.velocity:velocity:jar:1.7:compile
|  |     +- (commons-collections:commons-collections:jar:3.2.1:compile - omitted for duplicate)
|  |     \- (commons-lang:commons-lang:jar:2.4:compile - omitted for conflict with 2.6)
|  +- (org.apache.zookeeper:zookeeper:jar:3.4.5:compile - omitted for duplicate)
|  +- org.apache.thrift:libthrift:jar:0.8.0:compile
|  |  +- (commons-lang:commons-lang:jar:2.5:compile - omitted for conflict with 2.6)
|  |  +- (org.apache.httpcomponents:httpclient:jar:4.1.2:compile - omitted for duplicate)
|  |  \- (org.apache.httpcomponents:httpcore:jar:4.1.3:compile - omitted for conflict with 4.1.2)
|  +- org.jruby:jruby-complete:jar:1.6.5:compile
|  +- (org.mortbay.jetty:jetty:jar:6.1.26:compile - omitted for duplicate)
|  +- (org.mortbay.jetty:jetty-util:jar:6.1.26:compile - omitted for duplicate)
|  +- org.mortbay.jetty:jsp-2.1:jar:6.1.14:compile
|  |  +- org.eclipse.jdt:core:jar:3.1.1:compile
|  |  \- (org.mortbay.jetty:jsp-api-2.1:jar:6.1.14:compile - omitted for duplicate)
|  +- org.mortbay.jetty:jsp-api-2.1:jar:6.1.14:compile
|  |  \- (org.mortbay.jetty:servlet-api-2.5:jar:6.1.14:compile - omitted for duplicate)
|  +- org.mortbay.jetty:servlet-api-2.5:jar:6.1.14:compile
|  +- (org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile - omitted for duplicate)
|  +- (org.codehaus.jackson:jackson-mapper-asl:jar:1.8.8:compile - omitted for duplicate)
|  +- org.codehaus.jackson:jackson-jaxrs:jar:1.8.8:compile
|  |  +- (org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile - omitted for duplicate)
|  |  \- (org.codehaus.jackson:jackson-mapper-asl:jar:1.8.8:compile - omitted for duplicate)
|  +- org.codehaus.jackson:jackson-xc:jar:1.8.8:compile
|  |  +- (org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile - omitted for duplicate)
|  |  \- (org.codehaus.jackson:jackson-mapper-asl:jar:1.8.8:compile - omitted for duplicate)
|  +- org.slf4j:slf4j-log4j12:jar:1.4.3:compile
|  |  \- (log4j:log4j:jar:1.2.13:compile - omitted for conflict with 1.2.17)
|  +- (tomcat:jasper-compiler:jar:5.5.23:runtime - omitted for duplicate)
|  +- (tomcat:jasper-runtime:jar:5.5.23:runtime - omitted for duplicate)
|  +- org.jamon:jamon-runtime:jar:2.3.1:compile
|  +- (com.google.protobuf:protobuf-java:jar:2.4.0a:compile - omitted for conflict with 2.5.0)
|  +- (com.sun.jersey:jersey-core:jar:1.8:compile - omitted for conflict with 1.9)
|  +- (com.sun.jersey:jersey-json:jar:1.8:compile - omitted for conflict with 1.9)
|  +- (com.sun.jersey:jersey-server:jar:1.8:compile - omitted for conflict with 1.9)
|  +- javax.xml.bind:jaxb-api:jar:2.1:compile
|  |  \- javax.activation:activation:jar:1.1:compile
|  \- stax:stax-api:jar:1.0.1:compile
\- org.slf4j:slf4j-api:jar:1.7.5:compile

Java Command to run JAR

java -cp HBaseIntro-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.hbase_intro.HBaseIntro.HelloWorld arg1 arg2 arg3 agr4

Upvotes: 1

Views: 4501

Answers (2)

miljanm
miljanm

Reputation: 936

Slf4j had introduced some API incompatibilities between 1.5 and 1.6 versions. Newer versions are backwards compatible but if your application uses newer version, and older one gets resolved by maven you will get exactly the problem you are getting.

The easiest resolution for me was to force slf4j dependency version in dependency-management section. It is equivalent to Master Slave's answer, only it's using the mechanisms intended for that purpose.

It would look like this:

<dependencyManagement>
   <dependencies>
       <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.5</version>
       </dependency>
        <!--exchange this one for whatever backend you want-->
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.5</version>
       </dependency>
       <!--if you have dependencies that use commons-logging make sure that you exclude it from them and declare this one as well-->
       <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>jcl-over-slf4j</artifactId>
          <version>1.7.5</version>
       </dependency>      
   </dependencies>
</dependencyManagement>

Upvotes: 0

Master Slave
Master Slave

Reputation: 28569

You've probably didn't work out your exclusions completely. Even if so, you can pull your

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>

to be the first declared dependency in pom.xml, so that the latest jar is picked-up by the classpath.

Maven is using the "nearest-win" strategy, so if you have your desired dependency as a first declared, all the transitive, with the matched groupId and artifactId will be omitted.

You should explicitely define a binding as well (write below the slf4j-api), 'cause on a second look, its the binding that comes from the transitive dependencies, which would explain why your exclusions don't solve your problem. Not sure which binding you use but try adding something like

   <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>

and see if it helps

Upvotes: 2

Related Questions