1

I generated a jar file with Maven in IntelliJ IDEA. I get following error message when try to execute it:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

I use only one log4j version, 1.2.17 and java version 1.8.0_171. I set skip tests in pom.xml too: <maven.test.skip>true</maven.test.skip> Execution is successful directly from IDE. I use Maven Surefire plugin:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <skipTests>true</skipTests>
        </configuration>
    </plugin>
4
  • have you added jars with dependencies into classpath when executing your jar? Commented May 29, 2018 at 14:43
  • Are you trying to execute your Java program from the command line? Could you add the command you invoke? Commented May 29, 2018 at 15:17
  • @Daniele : I got following error, when try to execute with java -jar <myjar> : no main manifest attribute Commented May 30, 2018 at 6:51
  • you can fix the "no main manifest attribute" issue by using @streetturtle 's suggestion below. (the manifest will be added to the jar generated by mvn package). Commented May 30, 2018 at 12:02

1 Answer 1

1

When you run it from IDEA dependencies are included by IDEA, you can check the command in Run Tool Window of IDEA. But when you generate a jar and run it there is no dependencies included in it. To make it work you have to tell Maven to include them, one of the way to do it is to use Apache Maven Assembly Plugin by adding build section to your pom.xml:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>package.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>
Sign up to request clarification or add additional context in comments.

1 Comment

I added it, but it generated a 30 KB jar and got the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.