How to resolve module-info.java compile error in J

2019-02-17 07:56发布

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)

3条回答
2楼-- · 2019-02-17 08:35

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.

查看更多
Emotional °昔
3楼-- · 2019-02-17 08:42

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.

查看更多
家丑人穷心不美
4楼-- · 2019-02-17 08:51

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

查看更多
登录 后发表回答