2

I am trying to learn new concept of modules in JAVA. I am using Eclipse oxygen 4.7.3a with JDK 11.0.1.

I am getting syntax error warning on my module-info.java file. It seems that compiler is not able to parse new keywords of module-info.java file.

enter image description here

I performed following solution to fix this issue :

  1. Installed all JAVA 11 updates from Eclipse JAVA 11 updates URL : http://download.eclipse.org/eclipse/updates/4.9-P-builds

  2. Added JVM argument "--add-modules=java.se.ee" to eclipse.ini file.

enter image description here

But issue is still there . Any suggestions/inputs appreciated !

Naman
  • 27,789
  • 26
  • 218
  • 353
Gunjan Shah
  • 5,088
  • 16
  • 53
  • 72
  • Did you [report this to eclipse](https://wiki.eclipse.org/FAQ_How_do_I_report_a_bug_in_Eclipse%3F)? Also, what is the content of your `module-info.java` and where in the project structure is it placed? – Naman Oct 30 '18 at 03:35
  • You are using an Eclipse version that is outdated and older than Java 11. For Java 11, Eclipse 2018-09 plus the [_Java 11 Support for Eclipse 2018-09 (4.9)_](https://marketplace.eclipse.org/content/java-11-support-eclipse-2018-09-49) plugin are required. @nullpointer – howlger Oct 30 '18 at 08:00
  • @howlge, 4.7.3a is the stable and latest version. I cross checked here : https://www.eclipse.org/downloads/packages/release/oxygen . Is there any other latest updates after 4.7.3a ? – Gunjan Shah Oct 30 '18 at 09:24
  • @nullpointer, module-info.java file is empty only having module declaration. The same module snap is attached in the question. – Gunjan Shah Oct 30 '18 at 09:26
  • A new release ends the maintenance of the previous release. Oxygen was followed by Photon which was followed by 2018-09 (and Java 11 was released after 2018-09 was released): https://www.eclipse.org/downloads/packages/release – howlger Oct 30 '18 at 10:16
  • But modules were introduced in Java 9 and haven’t changed since then (as far as I know). So shouldn’t Eclipse be capable of compiling a `module-info.java`? – Holger Oct 30 '18 at 19:32
  • @nullpointer : this issue has already been reported to Eclipse community. Please refer this link : https://bugs.eclipse.org/bugs/show_bug.cgi?id=533390 – Gunjan Shah Nov 01 '18 at 04:43
  • I downloaded new Version: 2018-09 (4.9.0). And it is working fine with JDK 11. I downloaded "Eclipse IDE for Java EE Developers" from location : https://www.eclipse.org/downloads/packages/release/2018-09/r . And its working file with JDK 11. @Howlger : Could you please mention the same as answer, so I can mark this thread as Done. – Gunjan Shah Nov 01 '18 at 04:50
  • @Holger Yes, but if the compliance level is set less than 9, the file `module-info.java` is compiled as a regular Java file and `module` at the beginning is a syntax error according to the JLS. – howlger Nov 01 '18 at 08:12

1 Answers1

2

Java 11 requires

or Eclipse 2018-12 (4.10) which will be released on December 19, 2018.

Eclipse Oxygen was released before Eclipse Photon, Eclipse 2018-09 and Java 11 and therefore cannot detect a Java 11 JDK/JRE yet.

In addition, the Java compiler compliance level must set to 9 or higher (Project > Properties: Java Compiler) to have a module-info.java file with its own syntax which has been introduced with the Java Platform Module System (JPMS) in Java 9. If the compiler compliance level is lower than 9 the token module at the beginning of a .java file is a syntax error because module-info.java is a regular Java file in Java <= 8.

Please note that the setting in eclipse.ini refers to the JRE/JDK that is used to run Eclipse. The JREs or better (to see the source code) JDKs to use for development have to be specified in Window > Preferences: Java > Installed JREs.

howlger
  • 31,050
  • 11
  • 59
  • 99
  • Which makes me wonder, how this detection works. It seems astonishing unreliable, if it needs to be adapted for every new Java version. Here is, what I would do to detect a Java 9+ version: 1) use the `jrt-fs.jar` to open the contained module image (you’d need it anyway when you want to compile against the contained classes) 2) read the contained `java/lang/Object.class` and its class file version number. 3) subtract `44` from that number. For pre-Java 9 environments, you would have to open `rt.jar` to extract the class file. This works from JDK 1.1 to JDK12ea. So what does Eclipse do instead? – Holger Nov 01 '18 at 10:26
  • I guess the main problem is the version that is contained in the header of each `.class` file ([major version number: two bytes at offset 6 and 7](https://en.wikipedia.org/wiki/Java_class_file#General_layout)). Unfortunately, the Java class file version will be incremented with each Java version even if the class file format has not changed (as far as I know, that was the case with Java 10 (54) -> 11 (55)). Please note, a higher class file version can also mean that parts are not only unknown, but that the bytes are to be interpreted differently. – howlger Nov 01 '18 at 10:56
  • That’s exactly the version, I’m referring to. The minimum, Eclipse should understand, is that 55 > 54 (as said in my previous comment, I’d even go so far to assume that subtracting `44` gives us the major version, until recalled). So it should understand that a newer class file version than `53` allows compliance level `9` or `10`. If it fails to parse the class file, it’s an entirely different issue, as a compliance level of `8` or older won’t solve that problem. Compare with [with this known issue](https://stackoverflow.com/a/26105217/2711488). – Holger Nov 01 '18 at 11:04
  • @Holger This would only work if the class format was backward compatible, which it is by definition not. For example, if you ignore the version number, it could be that a file with version `60` can be read without problems, but incorrectly (because the individual bytes have different meanings). – howlger Nov 01 '18 at 11:13
  • As pointed out in the linked answer, Eclipse is *known for trying*, despite the higher number. If Eclipse just reported “*Unsupported JDK, version number xyz*”, no problem with that. But Eclipse doesn’t do that. It still tries and sometimes, it even works. But falling back to a compliance level before Java 9, because the version number is higher than 10, simply doesn’t make any sense. And it doesn’t change the chances of getting away with parsing JDK’s class files. The JDK’s class files do not change when you change your project’s compliance level. – Holger Nov 01 '18 at 11:30
  • @Holger _"But Eclipse doesn’t do that."_ Are you sure? I've already seen such errors. The compliance level is set in the _New Java Project_ dialog (by default, the previously selected value is preselected). Of course, in Eclipse Oxygen.3a you can fix the syntax error in `module-info.java` by setting the compliance level to 9, but this will not make the red exclamation mark on the project and the corresponding error in the _Problems_ view go away (which will be shown because of the unsupported JRE and which have not been mentioned/shown in the question). – howlger Nov 01 '18 at 11:57
  • This contradicts your answer “*…and therefore cannot detect a Java 11 JDK/JRE yet. The detection is required to be able to set the Java compiler compliance level to 9 or higher*”. Granted, the linked answer deals with Java 8, and things might have been changed since then. But this Q&A seems to be consistent with that. For all other features, e.g. trying to use lambda expressions with compliance level 7, Eclipse will issue an appropriate message that this doesn’t conform to the compliance level. It should do that for `module-info` as well. But if the JDK is unsupported, it should say that loud. – Holger Nov 01 '18 at 12:11
  • @Holger In fact, this was incorrect. Thanks for pointing this out. I have updated the answer. – howlger Nov 01 '18 at 12:35