0

I have a standalone component which is Maven project that uses its own independent Log4j2. I want to package that complete project in a JAR file. When I run mvn clean build command, it creates a JAR file in the 'target' folder but when I use this jar in another Java project, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
    at com.company.client.model.ConnectionClient.<clinit>(ConnectionClient.java:24)
    com.company.client.model.ConnectionClient.startXMLProcess(CallClient.java:25)
    at com.company.client.model.ConnectionClient.main(CallClient.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

But if I manually export that maven project to make an executable JAR and then use that JAR in another project, then it works absolutely fine.

So, I needed to know:

1) How can I generate a jar file for my component with the Log4j2 jars?

2) Am I right in assuming that I need to generate an executable JAR for my component so the generated JAR it can be used in other projects. If yes, then how can I do that in Maven.

Any help/advise will be appreciate.

Thanks

Global Dictator
  • 1,539
  • 9
  • 24
  • 37
  • 2
    possible duplicate of [how to build fat jar with maven](http://stackoverflow.com/questions/16222748/how-to-build-fat-jar-with-maven) – Joe Aug 13 '14 at 14:02
  • @Joe, no I dont believe its my question is a duplicate to the above as there are 2 aspects to my question and hence need more detailed description. Thanks – Global Dictator Aug 13 '14 at 15:19
  • 2) No -- you're wrong in your assumption. You only generate an executable jar if you want to run the jar separately. If your jar is to be used in other projects, as a dependency, it should not be executable, and it should not have log4j embedded within it either. – Software Engineer Aug 13 '14 at 15:50
  • @Engineer Dollery, So how can other projects use my component Jar? I have edited my original question for include the full error message that I get when other projects use my JAR generated by running mvn clean instal on my component – Global Dictator Aug 13 '14 at 16:07
  • See if you can obtain anything by playing with the [maven jar plugin](http://maven.apache.org/plugins/maven-jar-plugin/examples/attached-jar.html) – watery Aug 13 '14 at 16:30
  • Is the "other Java project" also a Maven project? – oberlies Aug 13 '14 at 16:53
  • Sorry, you need to learn a lot more about java development than we can provide answers for here. Your question is way too broad for this site. I'd start by reading the maven documentation. – Software Engineer Aug 13 '14 at 18:47
  • If you disagree that your question is a duplicate, edit your question to include a link to that other question and to explain why your question is not a duplicate. Rather than having a discussion in comments. – Raedwald Aug 14 '14 at 06:42

1 Answers1

1

Wouldn't transitive dependencies help. Your artifact depends on log4j2 and whoever uses your jars will automatically get log4j2 as a dependency.

Eventually if its a standalone application, that you have or someone whos using your jar builds, a self contained/fat executable jar needs to be build that will contain all relevant dependencies.

See the maven assembly plugin and/or maven shade plugin for creating those executable jars.

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
  • Thanks for your response. So, are you suggesting that I do need to create an executable jar? I was told above that I dont need to. This is one of the things I need clarification on whether or not I need to create an executable jar or not. Please clarify.. Thanks – Global Dictator Aug 14 '14 at 11:04
  • You dont need a executable jar if you dont intend to run your code in a standalone app. In your scenario, you had mentioned that someone else uses your jar/artifact. And they are running the code. In that case you either need a executable jar with all requisite deps. Or you can create the correct classpath (either env variable or passed to the JVM) and then you should be able to run your code. – Yogesh_D Aug 19 '14 at 03:47