7

Eclipse oxygen; windows 7; JDK 9 final from 9, 21; JUnit 4.12 and an existing application. As starting point the application can be compiled, executed and all JUnit Tests shows green. Now we use eclipse to generate the file module-info.java. The outcome looks like:

module ch.commcity.topsort {
   exports ch.commcity.topsort;

   requires junit;
}

But with the error: junit cannot be resolved to module. The question is: How to tell the file that junit does not define any module and it should be run in compatibility mode?

1
  • what framework are you using in your project? maven / gradle etc? Also which version of Eclipse oxygen?
    – Naman
    Sep 27, 2017 at 20:43

3 Answers 3

8

How to tell the file that junit does not define any module and it should be run in compatibility mode?

Your question seems to be based on several misconceptions:

  • You can not tell module-info.java whether JUnit defines a module or not. If a module says it requires another module then the compiler expects that module to be present - no way around that.
  • Whether JUnit 4 comes as a module or not is not overly important - as long as you place it on the module path, it will end up being treated as a module (possibly as an automatic one).
  • There is no "compatibility mode". You can continue to write code as you did before the module system (almost), but once you're creating module declarations you need to play by its rules.

I recommend to give the outstanding State of the Module System a thorough read and then ask yourself what exactly you are trying to accomplish. Are you really trying to create a module that depends on JUnit? Or was that just accidental because you use its API for testing. If the latter, you should not depend on it - instead your IDE / build tool needs to figure out how to compile and run your tests.

Expansion on "a module that depends on JUnit"

The module system does not classify dependencies as "compile" or "test" - if a module requires another module, it has to be present, always. That means a module that requires junit would force the presence of JUnit. Unless the module provides testing-related features, that is most certainly wrong.

In other words, requires junit is akin to adding JUnit to a project's POM, using the compile scope.

10
  • I'm sorry Nicolai, none of your assumption is correct. Look at my question. We have a little project "topsort". Built 10 years ago with Java 5 or 6. Now we have java 9. We compile and we run the JUnit tests to green. Fine! Now we go a step further. We use eclipse to generate the module-info.java file. This would allow us to have the topsort algorithm defined as a module for future use. So eclipse generate the file defining the module "topsort" and defining the dependency of JUnit for tests. But JUnit seems not to be on the module path. Why, and how to correct is the question.
    – juerg
    Sep 30, 2017 at 13:58
  • It would help if you could point out which of my "assumptions" are incorrect - all three bullet points are rock solid and the rest are largely questions. You write "So eclipse generate the file defining the module 'topsort' and defining the dependency of JUnit for tests" - unless topsort is a testing library, you don't want that dependency in there. I edited my answer to provide an explanation. Sep 30, 2017 at 14:26
  • I'm speaking about your assumptions w.r.t. my problem. You mention pom. It's not a maven project. An ant script exist to test and to build the jar. I repeat, the question is how to tell eclipse to put the JUnit library on the module path. I just read something about to toggle in the build path the entry Module:No to read Module:Yes. I tried that in the user Library definition of junit.jat and org.hamcrest.core_1.3.0v... I have found such an entry, but nothing can be toggled. I use the patches for Oxygen: 1.1.1.v20170826-0521_BETA_JAVA9 and 1.1.1.v20170826-0521_BETA_JAVA9.
    – juerg
    Sep 30, 2017 at 14:41
  • I do not assume you have a Maven project, I use it as an example to clarify why requiring junit is most certainly wrong (and we didn't even talk about the fact that no library should be published that references a module by the name derived from its file name). I did assume that "topsort" (can you link to the project?) is not a testing-related library. If that's correct, the answer stands. Sep 30, 2017 at 14:53
  • 3
    Short answer: remove requires junit; from module-info.java. Longer answer: Your test code does not belong in the resulting runtime jar. So your runtime jar in turn has no dependency on junit. Real answer: try to understand what Nicolai said, he is absolutely right and knows what he is talking about.
    – rgielen
    Oct 2, 2017 at 13:43
4

First, please update your Java 9 support for Eclipse Oxygen or use the latest available release candidate build for Eclipse 4.7.1a (to be released on October 11, 2017).

To add a library or a container to the modulepath of a Java 9 project in Eclipse, open the project's Java Build Path dialog. On the Libraries tab, select the node Modulepath and add your library to it. Make sure to first remove that library from the Classpath node if it is already present there.

As mentioned by others, in your case, the JUnit libraries will be considered as automatic modules.

Modulepath node

10
  • Be assured, I have the latest available BETA plugin for Java 9. I deteted the froject from eclipse, then I deleted all .x-files like .classpath, .project and the folder .settings on the HD. Then I reopened the project within eclipse. Resulting junit being seen by eclipse now!
    – juerg
    Oct 4, 2017 at 5:31
  • I also modified the eclipse.ini file to start with Java 9. This worked so far. I started also the JUnit tests from eclipse. something happened, but without showing a result, nor errors. I tried to use JUnit 5 instead of 4, but this seems to require more adaptions. W.r.t. Ant, ant still fails to compile the module-info.java. Infact, ant still use the Java 8 compiler. Though I tried to tell ant to use java 9 I didn't have success so far. Conclusion, Java 9, modules, + JUnit + ant, seems still to be a challenge. I'm not sure all this issues can be resolved until October 11.
    – juerg
    Oct 4, 2017 at 5:43
  • Your picture shows the subitem "Is modular" under Junit. This is missing in my IDE, though I can't get any newer BETA plugin. I have 1.1.1.v20170826-0521_BETA_JAVA9 installed. After 10.11 I'll download 4.7.1(a) and I'll see what changed. Until then I'll keep the other stones rolling up!
    – juerg
    Oct 4, 2017 at 6:07
  • @juerg "v20170826" implies that you have the changes from 26 August 2017. I am not sure why you are not able to get newer updates from October 2017. You should probably start with a fresh Eclipse installation and follow the steps from the Eclipse marketplace entry or just download the 4.7.1a RC2 build right now. If you still see any issues then I would suggest opening a bug report for Eclipse. Oct 4, 2017 at 6:16
  • Thanks for the confirmation. I agree. Yes I tried several times to update. Didn't work. I saw the download 4.7.1a RC2, but I didn't download. I'm waiting for 10.11 because I have also C++ plugins for JNI developments. This means some additional work, hence no time to do it twice. Anyway, the aim is to have some experiences with Java 9 and modules. I'll be back next week and if I see problems with my small nice already modular project from 2007, I'll report.
    – juerg
    Oct 5, 2017 at 8:51
0

How to tell the file that junit does not define any module and it should be run in compatibility mode?

Since the module junit as generated in the would be an automatic module converted from its artifact. You need to make sure that the jar for Junit junit:junit:4.12 is available on the modulepath of your project and the module would be resolved itself.

In order to make sure of the above, you can check dependencies of your project/module as configured in the IDE to include junit:4.12:jar.

3
  • JUnit is on the classpath of the IDE, otherwise the tests would not be green! But obviously it's not on the classpath w.r.t. module definition. Hence the question is how it can be checked, and where it can be configured. Of course I can wait until October 11 and the outcome of SP1 for Oxygen, hopefully then the bug is gone. Nevertheless I'm curious to know if it can be fixed before.
    – juerg
    Sep 28, 2017 at 6:44
  • 1
    It would have to be on the module path to become an automatic module. Sep 28, 2017 at 12:04
  • @Nicolai Yup, corrected. from classpath would anyway had been an unnamed module.
    – Naman
    Sep 28, 2017 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.