Bartłomiej Zieliński
Bartłomiej Zieliński

Reputation: 997

Eclipse - org.junit cannot be resolved, even though it's in the Maven dependencies

I've decided to write yet another question in this topic, as no solution from other posts worked for me.

My problem is that eclipse is unable to locate org.junit package at all, even though it is available in Maven dependencies and all necessary classes can be seen after expanding junit-4.12.jar.

Screenshot of the problem

Some technical info about the project and the environment:

Here's what I've already tried:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my-group-id</groupId>
    <artifactId>my-artifact-id</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>my-name</name>
    <url>http://maven.apache.org</url>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
    </dependencies>
    <modules>
        <module>module.1</module>
        <module>module.2</module>
        <module>module.3</module>
        <module>module.4</module>
        <module>module.5</module>
        <module>module.6</module>
        <module>module.7</module>
    </modules>
</project>

...and subproject's POM looks like this:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>my-group-id</groupId>
        <artifactId>my-artifact-id</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>module.4</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-module-4-name</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

As seen on the screenshot, the class is in the correct folder (src/test/java), so it's not a scope problem.

I think it's also worth mentioning, that:

Upvotes: 4

Views: 9133

Answers (4)

Lokesh Petluru
Lokesh Petluru

Reputation: 1

Add the dependency

   <dependency>
        <groupId>junit</groupId>
        <artifactId>junit-dep</artifactId>
        <version>4.8.2</version>
    </dependency>

it worked.

Upvotes: -2

Adarsh Shetty
Adarsh Shetty

Reputation: 81

My workspace projects had the same problem identifying junit and other libraries after upgrading to Eclipse Photon. What worked for me was Maven > Update Project (you can select all open projects that has the issue). My projects were all in Java 8.

Upvotes: 8

mikes
mikes

Reputation: 2353

Configuration seems fine.

Please check the jar file in your repository c:\users\bartl\.m2\repository\junit as this file may be broken. In my case maven uses another repository different from that in the user's home directory so maven was able to compile successfully.

I suggest deleting the whole junit directory in the repository and let eclipse download the files again.

I experience such behavior sometimes when there is a poor internet connection or no internet connection at all.

Upvotes: 2

p__Z
p__Z

Reputation: 46

The problem could be related to the thing how non-modular jars are managed in Java 9. The automatic modules are created for every non-modular java application which doesn't have support for Java 9 and their name is created based on the name of the non-modular jar. In order to fix that, could you please add "requires junit" to your module-info.java?

requires junit;

Upvotes: 3

Related Questions