Search code examples
javaeclipsejava-9java-moduleeclipse-classpath

How to use the .classpath of a modular project in Eclipse?


I created a test project with the following structure:

baseTest
   |- src/main/java
   |  |- com.base
   |  |  |- MainDependency.java
   |  |- module-info.java
   |- src/test/java
      |- test.com.base
         |- SomeTest.java

module-info.java is just an empty module declaration:

module com.baseTest {
}

Same for MainDependency.java:

package com.base;

public class MainDependency {
}

SomeTest is a class relying on code from main and from the JDK:

import java.util.logging.Logger;

import com.base.MainDependency;

public class SomeTest {

    String s;
    MainDependency md;
    Logger j;
}

The .classpath is

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" path="src/test/java"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
        <attributes>
            <attribute name="module" value="true"/>
            <attribute name="add-exports" value="java.logging/java.util.logging=ALL-UNNAMED"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="bin"/>
</classpath>

Using the above configuration, I get an The import java.util.logging cannot be resolved error. Removing <classpathentry kind="src" path="src/main/java"/> removes the error (probably because the project isn't considered modular anymore).

Iv'e found bug 526831 in Eclipse, but it seems from there that it isn't really a bug(?). Am I doing something wrong with regards to the project setup or configuration?


Solution

  • In module-info.java the required module java.logging has to be specified (instead of <attribute name="add-exports" value="java.logging/java.util.logging=ALL-UNNAMED"/> in .classpath):

    module com.baseTest {
        requires java.logging;
    }
    

    Eclipse provides a Quick Fix (Ctrl+1) for that: Add 'requires java.logging' to module-info.java.