7

I am trying simple Maven app with Log4J ver 2-beta 9. In my pom.xml file I have these two dependencies (as is mentioned in Log4J Maven webpage):

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-beta9</version>
</dependency>
<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-beta9</version>
</dependency>

Eclipse sees Log4J library:

enter image description here

But when I package the app and run it this Exception is thrown:

java -cp target/notification-1.0.0.jar com.example.Sandbox

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
    at com.example.Sandbox.<clinit>(Sandbox.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.LogManager
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

I searched for this Exception and it seems to be related to CLASSPATH variable.

How should be CLASSPATH set for Maven project?

user2148736
  • 1,173
  • 3
  • 19
  • 39
  • it could be like more log4j jar available in your class path and its getting confilcted – shreyansh jogi Nov 18 '13 at 10:10
  • Yes, my own another project what is dependency for this application uses Log4J too, can be this a problem? I'm not sure. – user2148736 Nov 18 '13 at 10:16
  • In Maven a jar which is created by `mvn package` does not contains it's dependencies. So you have to add the log4j-* to your classpath. You might check things like maven-assembly-plugin to create a jar-with-dependencies. – khmarbaise Nov 18 '13 at 10:17

1 Answers1

7

Maven is a build tool. It doesn't help you much running the final application.

You can use mvn exec:java -Dexec.mainClass="com.example.Sandbox" to run your app (see the question Maven Run Project) but this gets tedious when you have to pass arguments to it.

You can get the classpath that Maven used to compile your application with mvn dependency:build-classpath

That will print the classpath to the console. Note that it will be missing target/notification-1.0.0.jar

Another useful tool in this area is the assembly plugin; it will create one very big JAR with all the dependencies rolled into a single file when you specify the descriptor jar-with-dependencies.

Community
  • 1
  • 1
Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
  • I have tried Maven Exec Plugin and the app is running correctly, thanks. It means that my app doesn't find dependencies. I installed maven-dependency-plugin to copy all dependencies to ${project.build.directory}/lib. I think that the last think I need to solve is to set class path to this directory. How it can be solved when I run my application at productive environment? Is it save to copy all target directory and set classpath to "./lib" dir? – user2148736 Nov 18 '13 at 12:22
  • You need to set the classpath to `"lib/*"` in this case; don't forget the quotes, you don't want the shell to expand the asterisk! You must pass it to Java. http://stackoverflow.com/questions/1237093/using-wildcard-for-classpath – Aaron Digulla Nov 18 '13 at 12:44