0

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?

user1803551
  • 12,965
  • 5
  • 47
  • 74
  • 2
    You run in [Eclipse bug 525948](https://bugs.eclipse.org/bugs/show_bug.cgi?id=525948) which is already fixed but will be released with Eclipse Oxygen.3 on March 21 (in the meantime, you can use a nightly or milestone build). – howlger Jan 28 '18 at 10:03
  • @howlger I tried 4.8M5 and there's the same problem. – user1803551 Jan 28 '18 at 12:37
  • Make sure, to fix the _Java Build Path_ of your project first (create a new project to see how a correct `.classpath` should look like). – howlger Jan 28 '18 at 12:46
  • @howlger Did that, don't see any difference. – user1803551 Jan 28 '18 at 13:35
  • Your `.classpath` says JUnit 5 (`org.eclipse.jdt.junit.JUNIT_CONTAINER/5`), but your Java code says JUnit 4 (`import org.junit.Test;` instead of `import org.junit.jupiter.api.Test;`). – howlger Jan 28 '18 at 13:46
  • @howlger It doesn't matter if I use 4 or 5 on the classpath. – user1803551 Jan 28 '18 at 13:49
  • 1
    You should get a different error when adding JUnit to the modulepath instead of the classpath. As workaround for the error that is caused by the JUnit JARs, you have to add the JUnit 5.0.2 or 5.0.3 JARs manually to your project. See [Eclipse bug 529120](https://bugs.eclipse.org/bugs/show_bug.cgi?id=529120) which has not yet been fixed. I would recommend using `Automatic-Module-Name` instead of having a `module-info.java` (which adds no value IMHO). – howlger Jan 28 '18 at 15:12
  • See also https://stackoverflow.com/a/46289257/6505250 – howlger Jan 28 '18 at 21:57
  • @howlger This isn't it. Iv'e edited the question to remove the JUnit dependency because it seems to be confusing and used different one (I did write that it doesn't matter which dependency it is). – user1803551 Jan 30 '18 at 08:10
  • Your structure shows that `module-info.java` is in the folder `source/` which is not a source folder. Your `.classpath` file shows that there is a folder `src/` which is also not a source folder (`kind="src"`). – howlger Jan 30 '18 at 09:27
  • @howlger Sorry, the structure was written by hand so it had a typo, it's `/src`. Should be correct now. – user1803551 Jan 30 '18 at 09:34
  • `module-info.java` must be either in `src/main/java` or in `src/test/java` (not in `src`/`source`) – howlger Jan 30 '18 at 09:47
  • @howlger Apparently I didn't press "save edit". It is under `src/main/java`. – user1803551 Jan 30 '18 at 09:58
  • I guess `SomeMock` (third paragraph) is `SomeTest` (structure), right? What do you exactly mean by _"an empty module declaration"_? – howlger Jan 30 '18 at 10:10
  • @howlger Yes, and I added the module file. – user1803551 Jan 30 '18 at 10:13
  • I think you asked something else. However, see my answer to your current question below. – howlger Jan 30 '18 at 11:32

1 Answers1

1

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.

howlger
  • 31,050
  • 11
  • 59
  • 99
  • But `test` does not define a module, so it's in the unnamed module. Requiring a module for `main` shouldn't matter. – user1803551 Feb 01 '18 at 10:51
  • `module-info.java` is for both packages, independent in which source folder it is located. Currently, Eclipse supports only one module per projects. If Eclipse would allow multiple modules (one module per source folder), different output folders per source folder would be required (instead of only `bin` as in your example). – howlger Feb 01 '18 at 11:02
  • It's already possible to define a different output folder in 3.8 (and mark the test source with the `test` attribute), but it it doesn't appear to change anything. – user1803551 Feb 01 '18 at 13:26
  • Yes, a project can have multiple output folders but not (yet) multiple modules (I can't find the Eclipse bug for this improvement right now). – howlger Feb 01 '18 at 13:39
  • OK, so ultimately the answer is that Eclipse doesn't support the full specs yet and and the workaround is doing what you suggested in your answer. Yes? – user1803551 Feb 01 '18 at 13:47
  • There is nothing in the [Java Language Specification](https://docs.oracle.com/javase/specs/jls/se9/html/index.html) about projects. Eclipse supports multiple modules but currently, one project is required for each module. Eclipse is developed with Eclipse and also here a project cannot contain more than one OSGi bundle (alias Eclipse plug-in). If you come from the Maven world, you would certainly expect an additional folder level for modules. If you came from the Eclipse world, however, you would find Maven's deeply nested folder structures complicated. – howlger Feb 01 '18 at 14:18