2

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.

noobcoder
  • 11,983
  • 10
  • 39
  • 62
  • Your pom snippet shows mostly compile time dependencies (because that the default scope). However I guess that the classnotfound exception appears in the runtime environment. Could you please check whether the commons-logging libraries are in the classpath of your target environment? – chris.tian Feb 25 '14 at 20:45
  • Please paste the part of the pom, that shows the assembly of the final build artifact – chris.tian Feb 25 '14 at 20:49
  • Do you really need 3 logging frameworks and a bigdata framework to print the sum of 2 integers on the console? – JB Nizet Feb 25 '14 at 20:49
  • This is a part of a bigger task. I am trying out from a smaller task since I am learning this thing. – noobcoder Feb 25 '14 at 20:52

2 Answers2

7

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.

Michael
  • 301
  • 3
  • 8
1

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>
chris.tian
  • 770
  • 5
  • 13
  • Still facing the same issue :( – noobcoder Feb 25 '14 at 22:26
  • Are all the libraries you need in the same folder? Does the file META-INF/manifest in your addition-examppe-0.0.1-SNAPSHOT.jar list all of these libraries? – chris.tian Feb 26 '14 at 05:55
  • Yes. All libraries are in the same folder. Moreover, I am able to run the command in the eclipse editor by using Run as > Run Configurations and under aruguments I simply put my command name with the two arguments as follows: "addition 1 2" and that works fine and returns the sum. The problem occurs only when I run the command through command line without the editor. – noobcoder Feb 26 '14 at 06:39
  • Does this command work for you? java -cp "*" addition 1 2 -dataType Integer – chris.tian Feb 26 '14 at 06:44
  • Nope. The command would give me an error saying: Error: Could not find or load main class addition – noobcoder Feb 26 '14 at 06:47
  • Do you need to add a package name or is your class *addition* is in the default package? – chris.tian Feb 26 '14 at 06:49
  • I don't think so. The code for addition command is in a package that is defined by me – noobcoder Feb 26 '14 at 06:54
  • Then please try this: java -cp "*" your.package.name.addition 1 2 -dataType Integer I am just trying to figure out whether it is a classpath issue. Because in Eclipse the classpath is probably different. – chris.tian Feb 26 '14 at 06:57
  • I think it is a classpath issue. But I am not sure as to how do I resolve that issue. PArdon me, but I am new to all the concepts and my questions are too naive. – noobcoder Feb 26 '14 at 07:25