There is a project maven, it is compiled in Intelli Idea. After that, I compile it with maven and create a jar file. Which at start (in the console) gives an error:

java.lang.NoClassDefFoundError: org / apache / log4j / Logger at

Main. (Main.java:10)

Swears on logging, works without logging and writes what I need.

XML file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Time</groupId> <artifactId>GreetTime</artifactId> <packaging>jar</packaging> <version>1.0</version> <name>GreetJar</name> <url>http://maven.apache.org</url> <properties> <jdk.version>1.6</jdk.version> <log4j.version>1.2.12</log4j.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <mainClass>Main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> <pluginRepositories> <pluginRepository> <id>onejar-maven-plugin.googlecode.com</id> <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url> </pluginRepository> </pluginRepositories> 

Program Code:

 import org.apache.log4j.Logger; import java.util.ResourceBundle; /** * Created by Alex on 07.09.2016. */ public class Main { public static Logger log = Logger.getLogger(Main.class); public static void main(String[] args) { log.info("Create Greet object"); Greet gr = new Greet(); gr.getLocale(); gr.gethours(); ResourceBundle bundle = ResourceBundle.getBundle("Bundle", gr.myLocale); log.info("Switch language"); gr.showMessage(bundle); log.info("Conclusion greetings"); log.info("Terminate program"); } } 

Judging from what I found on the Internet, I need to correct the haze in the mainfest and somehow create lo4j.jar, it remains to understand how.

    1 answer 1

    The problem is that loj4j.jar does not lie next to your jarn and is not listed in the classPath.

    1) can be added to the classPath as a key, or in the manifest

     Сlass-Path: log4j.jar 

    2) add to pom

     <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> 

    execute the mvn package command, you will collect 2 files in the target : one with all dependencies ( GreetTime-1.0-jar-with-dependencies.jar ) and the other without them ( GreetTime-1.0.jar )

    the one that will work with all dependencies and will not require adding libraries to the classPath

    • When I run a file with dependencies, I get this no main manifest attribute, in D: \ Programming \ Projects \ Java \ Greeting \ target \ GreetTime-1.0-jar-with-dependencies.jar So there was a problem with the manifest, tell me how to make it right ? here is my Manifest-Version: 1.0 Class-Path: log4j.jar Main-Class: Main - AlexanderBogomaz
    • With this introduction of the command, it started java -cp GreetTime-1.0-jar-with-dependencies.jar Main - AlexanderBogomaz
    • @AlexanderBogomaz no problem now? - Senior Pomidor
    • No, no, in fact everything works, you just have to write the name of the class at the end, can you tell me how to run the tests while building? Here or I created a new question. [ Ru.stackoverflow.com/questions/567253/… - AlexanderBogomaz