2

I downloaded eclipse photon and tried to run very basic example with log4j2. Here is my configuration

Project structure

Project Structure

Here is the module path screen shot

Module Path screen shot

POM.xml

<properties>

    <java-version>10</java-version>

    <!-- Unit testing -->
    <junit.version>4.12</junit.version>
    <junit-jupiter-engine.version>5.2.0</junit-jupiter-engine.version>
    <junit-platform-runner.version>1.2.0</junit-platform-runner.version>

    <!-- Logging -->
    <log4j.version>2.11.0</log4j.version>
    <slf4j-api.verion>1.7.25</slf4j-api.verion>
    <jboss-logging.version>3.3.2.Final</jboss-logging.version>

    <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
    <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
    <maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
    <maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

    <!-- Unit Testing  -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit-jupiter-engine.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit-platform-runner.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- Logging-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jul</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j-api.verion}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>${jboss-logging.version}</version>
        <scope>runtime</scope>
    </dependency>

</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <pluginManagement>
        <plugins>

            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-enforcer-plugin</artifactId>
                                    <versionRange>[1.0.0,)</versionRange>
                                    <goals>
                                        <goal>enforce</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>${maven-javadoc-plugin.version}</version>
            <configuration>
                <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
        </plugin>
    </plugins>
</build>

Main Class

package pk.training.basit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Welcome {

    private static final Logger logger = LogManager.getLogger();

    public static void main(String[] args) {
        logger.info("Welcome to the Module System.");

        // Print the module name of the Welcome class
        Class<Welcome> cls = Welcome.class;
        Module mod = cls.getModule();
        String moduleName = mod.getName();
        System.out.format("Module Name: %s%n", moduleName);
    }
}

module-info

module pk.training.basit {
    exports pk.training.basit;
    requires log4j.api;
}

But when I run the project I am getting error that

Error occurred during initialization of boot layer
java.lang.module.FindException: Module log4j.api not found, required by pk.training.basit

Why I am getting this error? Dependencies are in the module path.

If I remove the log4j2 from the code and then run the code with the following module-info:

module pk.training.basit {
    exports pk.training.basit;
}

Then code runs fine.

So is there something that I am doing wrong or it's maven eclipse related problem that it can't find third party dependencies?

Thanks

Basit
  • 8,426
  • 46
  • 116
  • 196

2 Answers2

1

The name of the log4j api module shown above (log4j.api) is wrong. The module name for Log4j API is org.apache.logging.log4j.

rgoers
  • 8,696
  • 1
  • 22
  • 24
  • If you use `requires org.apache.logging.log4j;` in module-info. In eclipse photon you are getting error `org.apache.logging.log4j cannot be resolved to a module`. If you don't use `requires log4j.api;` in module-info. Then in the java file you are getting error `The import org.apache.logging.log4j.LogManager cannot be resolved`. But if you use `requires log4j.api`. You are getting no module find error. – Basit Jul 11 '18 at 11:28
  • rgoers is correct, see the [source code](https://git-wip-us.apache.org/repos/asf?p=logging-log4j2.git;a=blob;f=log4j-api-java9/src/main/java/module-info.java;hb=559e1de809b407aa43080efa61b9c83ebde084b9) and the [manual](https://logging.apache.org/log4j/2.x/runtime-dependencies.html). Looks like the API has a separate artifact for java9 called log4j-api-java9 so I suggest you try using this artifact id – D.B. Jul 14 '18 at 18:01
  • log4j-api-java9 isn't a separate artifact. Most of Log4j is compiled with Java 7 but those classes have to be compiled with Java 9. During they build the classes are copied into the log4j-api jar. Note that log4j-api is a multi-release jar and all the classes, including module-info, are in META-INF/versions/9. – rgoers Jul 15 '18 at 21:58
0

I've got the exact same problem.

As of version 2.10.0 the Log4j API is a Java module (with a module-info.java)

when typing org.apache.logging.log4j eclipse propose me to complete it with core. for the core plugin of the log4j2 app. it doesn't change anything to the problem

the only solution yet is to break the modularity by removing log4j-api from module-info.

if I keep log4j.api in the module-info I also get this message on my imports:

The package org.apache.logging.log4j is accessible from more than one module: log4j.api, org.apache.logging.log4j.core

and the class I'm looking to import is not in log4j.core

yboompook
  • 21
  • 5
  • when adding `-p C:\Users\kevin\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.0 -m log4j-api/org.apache.logging.log4j` I've got this message instead `java.lang.module.FindException: Unable to derive module descriptor for C:\Users\kevin\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.0\log4j-api-2.11.0-sources.jar Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.apache.logging.log4j.util.EnvironmentPropertySource not in module` – yboompook Jul 06 '18 at 11:59