简体   繁体   中英

java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl

I am writing a command for a maven project in Java using the 'CommandLine' class. The command will take two integer values as parameters on a command line and displays its sum. However, my project does not build and throws exception as follows:

java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl

I did some research on the error, and I have added the following dependency to my pom.xml file

    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase</artifactId>
        <version>0.94.2-cdh4.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
<dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>2.0.0-mr1-cdh4.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.0.0-cdh4.2.1</version>
    </dependency>
    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>jdk.tools</groupId>
        <artifactId>jdk.tools</artifactId>
        <version>1.7.0_05</version>
        <scope>system</scope>
        <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
    </dependency>

The command I run on my command prompt is as follows:

java -jar addition-examppe-0.0.1-SNAPSHOT.jar addition 1 2 -dataType Integer

However, I still get the same error on my console as I mentioned above. Any other work around for this particular problem ?

Note: This command is built by me so I have written the Java classes and methods for the operation but the error is simply because of a maven dependency.

Perhaps, this question is still actual...

I've got the same problem using Apache httpclient 4.4 . I use maven-shade-plugin 2.3 to build the Uber-JAR and my (parent) module configuration included <minimizeJar>true</minimizeJar> in the section of the shade plugin. It worked fine for all modules until I added httpclient dependency into one of them and got that error.

I added dependencies on commons-logging , log4j , and logkit and set <minimizeJar>false</minimizeJar> in the POM of the module that depends on the httpclient . The error has gone, but I have a warning at runtime:

log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Looks like this is CloseableHttpClient problem.

I do not use the logging at all, but it seems that InternalHttpClient class requires the logger anyway.

Most probably the libraries you depend on are not available in the classpath of the artifact, which you try to execute. You will need to add the assembly plugin to your build section. Furthermore you will need to configure the jar-plugin to add all dependencies to the manifest and thereby extend the classpath.

Luckily, for you I have the snippets which you need to add to your pom.xml. These change will copy all libaries to the target directory of your project:

<build>
....
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
                            <version>2.4</version>
            <executions>
                <execution>
                    <id>assembly:package</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <!-- The filename of the assembled distribution file default ${project.build.finalName} -->
                        <finalName>${project.build.finalName}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>

        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
                            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Furthermore you should create the file src/assembly/assembly.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>package</id>
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>


<fileSets>
    <fileSet>
        <directory>target</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
</fileSets>

<dependencySets>
    <dependencySet>
        <outputDirectory></outputDirectory>
        <outputFileNameMapping>
            ${artifact.artifactId}-${artifact.version}.jar
        </outputFileNameMapping>
        <unpack>false</unpack>

    </dependencySet>
</dependencySets>

</assembly>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM