14

I have a Maven project that uses Spring to run a json web service. The project runs fine in NetBeans. It gets compiled into a jar file, and the jar file runs fine in an Ubuntu VM. However, when I try to run the jar file in Windows, I get the following error.

java -jar myjar.jar --server.port=8000

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.springframework.core.io.support.SpringFactoriesLoader.<clinit>(SpringFactoriesLoader.java:58)
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:368)
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:359)
        at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:230)
        at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:206)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
        at com.baselayer.dal.core.Application.main(Application.java:20)
        ... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 14 more

I have spend an entire day trying to figure this out, to no avail.

Can anyone shed any light?

This is my pom.xml below.

<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.baselayer</groupId>
    <artifactId>dal</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Baselayer Core DAL</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
    </parent>


    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>     
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>        
        <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.1.1</version>
          <scope>provided</scope>
        </dependency>      
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-cassandra</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>antlr</artifactId>
                    <groupId>org.antlr</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>metrics-core</artifactId>
                    <groupId>com.yammer.metrics</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>liquibase-core</artifactId>
                    <groupId>org.liquibase</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-mapping</artifactId>
            <version>2.1.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-asl</artifactId>
                <version>1.9.2</version>
                <type>jar</type>
        </dependency>        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
    </dependencies>    
</project>
Mark Nugent
  • 599
  • 2
  • 5
  • 20
  • If I extract the contents of the jar file and look at the MANIFEST.MF, it does not contain a class path. Could this be why it cannot find the commons-logging library? – Mark Nugent Jan 13 '15 at 18:30

2 Answers2

20

Spring Framework uses Apache Commons Logging API for logging. In Spring Boot parent POM there is an explicit exclusion of the Commons Logging library.

<exclusion>
    <artifactId>commons-logging</artifactId>
    <groupId>commons-logging</groupId>
</exclusion>

This means that it's left to you to provide a chosen Commons Logging Implementation to Spring classes at runtime.

You can add commons-logging to your POM, latest version is 1.2, released in July 2014.

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

Or you can bridge Apache Commons Logging API to SLF4J Api (see http://www.slf4j.org/legacy.html) with the jcl-over-slf4j.jar and then add an SLF4L implementation of your choice. For example:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
</dependency>
Lorenzo Conserva
  • 1,026
  • 11
  • 9
  • jcl-over-slf4j is slf4j bridge. Next, you can add say, logback, which is an implementation, instead of slf4j-simple. The slf4j-simple above is an slf4j implementation. – Apurva Singh Oct 11 '17 at 15:23
2

You included the following on your commons-logging dependency:

<scope>provided</scope>

This is an instruction to prevent commons-logging from being included in your jar. Hence it not being on the classpath.

Steve
  • 9,270
  • 5
  • 47
  • 61
  • I removed the provided from the pom.xml and recompiled the jar file, but I still get the same error. I did "jar tf" on the new jar file and it shows the commons-logging-1.1.1.jar is contained in my jar. – Mark Nugent Jan 13 '15 at 17:26
  • You're using Spring Boot, so you shouldn't actually need to add any logging dependencies. In fact, you seem to be trying to override the built in commons logging dependencies with a lower version. Try removing that dependency completely. – Steve Jan 14 '15 at 09:06
  • I removed the dependency, but I still get the same error. – Mark Nugent Jan 15 '15 at 19:52