How to resolve module-info.java compile error in Jdk9/java-9
Asked Answered
S

4

10

I am trying to run below code using jdk-9 but facing problem when compile using command

Command

 javac -d mods .\module-info.java com\nirav\modi\Test.java

Error

.\module-info.java:1: error: class, interface, or enum expected
module module1 { }
^
1 error

module-info.java

module module1 { 

}

Test.java

package com.nirav.modi;

class Test {

    public static void main(String args[]){

        System.out.println("Hello Modular...");

    }

}

package structure is like below

module1\module-info.java
module1\com\nirav\modi\Test.java

JDK Version

java version "9-ea"
Java(TM) SE Runtime Environment (build 9-ea+153)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+153, mixed mode)
Seaddon answered 25/1, 2017 at 10:35 Comment(0)
O
11

Per the Jigsaw Issues List, a module name cannot end with a number. The purpose is to discourage authors from encoding version numbers in module names.

Oshaughnessy answered 25/1, 2017 at 18:44 Comment(4)
Note that there is currently (see this comment timestamp) a proposal to "walk back" the restriction. (Personally, I hope it fails ... but its not my call.)Charkha
@StephenC After broad feedback, the specification was revised to allow digits at the end of module names: openjdk.java.net/projects/jigsaw/spec/issues/…Requisition
What is the purpose of jigsaw then? The only problem I had with Java packages from the pre-module era was the lack of version handling. If jigsaw doesn't solve it, moreover actively prevents workarounds, then what is the point?Mediate
@TamasHegedus The module system intended to leave version resolution to the build tools. What the module system gives you is compile time errors about loading modules instead of runtime errors about loading packages/classes. The other thing Jigsaw gives you is the ability to use jlink to make a customized JVM runtime since the JDK itself is modularized. For more info, my favorite answer to this exact question in SO is here: https://mcmap.net/q/240956/-why-project-jigsaw-jpmsOshaughnessy
O
3

JSR 376 is not final yet and there are several topics still under discussion. The latest proposal on the #VersionsInModuleNames topic is here:

http://mail.openjdk.java.net/pipermail/jpms-spec-experts/2017-March/000659.html

Ovariotomy answered 26/3, 2017 at 8:32 Comment(0)
A
2

As per JDK-8160181 it is not forbidden to use digits in module names, however they should not be related to versioning.

Examples:

  • log4j - OK
  • java.compact3 - OK
  • guava19 - NOT OK - 19 is related to major guava version
  • module1 - OK IF 1 doesn't mean a version but one of many modules in a sample project (better name should be used in real projects)

Since Lint can't figure out what the digit means it reports digits by default and you can suppress them in module file with:

@SuppressWarnings("module") // 1 in module1 is module number, not version
module module1 {
...
}

The comment is optional but explains "why" the warning was suppressed

Ayer answered 30/4, 2020 at 10:37 Comment(0)
L
1

Seems like you are compiling with non-jigsaw jdk build here... here is the link were they are jigsaw

With that compiler these same samples that you have compile just fine on my laptop.

This is how java -version looks like for project jigsaw:

java version "9-ea"
Java(TM) SE Runtime Environment (build 9-ea+153-jigsaw-nightly-h6003-20170124)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+153-jigsaw-nightly-h6003-20170124, mixed mode)

EDIT It works with both jigsaw and non-jigsaw build. I have just tried it (build 149)

EDIT2 So i've tried with jdk-9 build 153 and here is what I got:

javac -d mods module-info.java Modules.java
module-info.java:1: warning: [module] module name module1 should avoid terminal digits
module module1 {
      ^
1 warning

removing the "1" and running the same command compiled OK and mods directory was created with both class files compiled just fine.

Landes answered 25/1, 2017 at 10:52 Comment(4)
Are there two jdk for java-9? like normal and with modularity?Seaddon
Most modularity features can also be found in the "regular" build. @Eugene: Did you try whether it really doesn't work on one of those? (No time to try out now.)Barter
@Nicolai my fail.. it works indeed with the simple build. I'll update the answerLandes
@Landes I had tried with jigsaw build which you had suggested but that still not worked for me. Need to check with after removing the number from the module name.Seaddon

© 2022 - 2024 — McMap. All rights reserved.