2

Import of a Java-9-Jigsaw-Maven-Project in Eclipse Oxygen 4.7 does not work

I use:

  • JDK 9 build 9-ea+172
  • Maven 3.5.0
  • Eclipse Oxygen 4.7 RC3 Version 4.7.0.I20170531-2000 from 2017-05-31
  • Eclipse-Plugins:
    • Eclipse JDT (Java Development Tools) Patch with Java 9 support (BETA) for Oxygen development stream, 1.1.1.v20170526-0728_BETA_JAVA9
    • m2e - Maven Integration for Eclipse (includes Incubating components), 1.8.0.20170516-2043

Creata a new Jigsaw-Maven-Project:

mkdir proj1\a\src\main\java\a\a
cd proj1

In directory proj1 the file pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>Proj1</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>a</module>
  </modules>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>9</source>
          <target>9</target>
          <showWarnings>true</showWarnings>
          <showDeprecation>true</showDeprecation>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

In directory proj1\a the file pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>test</groupId>
    <artifactId>Proj1</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>a</artifactId>
  <packaging>jar</packaging>
  <build>
    <sourceDirectory>src/main/java/a</sourceDirectory>
  </build>
</project>

In directory proj1\a\src\main\java\a the file module-info.java:

module a { }

In directory proj1\a\src\main\java\a\a the file App.java:

package a;

public class App {
   public static void main( String[] args ) {
      System.out.println( "CLASSPATH:           " + System.getProperty( "java.class.path" ) );
      System.out.println( "Class / Modul:       " + App.class.getSimpleName() + " from " + App.class.getModule() );
      java.lang.ModuleLayer lr = App.class.getModule().getLayer();
      if( lr != null ) {
         System.out.println( "Layer.Configuration: " + lr.configuration() );
      } else {
         System.out.println( "Error:               ModuleLayer is null" );
      }
   }
}

Running the project on command line:

cd proj1
mvn clean package
java -p a\target\a-1.0.jar -m a/a.App  

-->

CLASSPATH:
Class / Modul:       App from module a
Layer.Configuration: java..., ...  

-->
Works perfect without error (CLASSPATH is empty, name from getModule() is correct, and ModuleLayer is valid).

Opening the project in IntelliJ IDEA 2017.2 EAP
-->
Works perfect without error (CLASSPATH is empty, name from getModule() is correct, and ModuleLayer is valid).

Importing the project in Eclipse Oxygen 4.7 RC3:
-->

CLASSPATH:           ...\proj1\a\target\classes
Class / Modul:       App from unnamed module @68f7aae2
Error:               ModuleLayer is null

-->
All three lines are wrong.

How can I avoid this errors?

  • Why have you configured `src/main/java/a`? – khmarbaise Jun 03 '17 at 14:33
  • Apart from that the `module-info.java` file must be located into `src/main/java`... – khmarbaise Jun 03 '17 at 14:55
  • Hi Karl Heinz, there exist a recommendation: "`By convention, the source code for the module is in a directory that is the name of the module`", see: http://openjdk.java.net/projects/jigsaw/quick-start. For this you have to do: a) place `module-info.java` in this directory (in my case in `src/main/java/a`), and b) configure in Maven-POM "src/main/java/a... This project directory layout is correct. It has the advantage that you can easily add further Jigsaw-Modules in the same `src/main/java` directory. – Torsten Horn Jun 03 '17 at 18:49
  • This convention is only for plain java usage not related to Maven. In the end a `module-info.class` will end up in the [root of a jar file](http://openjdk.java.net/projects/jigsaw/spec/sotms/#module-artifacts) which contains the appropriate module and that means you can't have multiple module files in a single Maven module. Having multiple jigsaw-modules within the same `/src/main/java` will not really work cause that means you need to create two different jar files one for each module. That is the same issue trying to create two different jar's from that same maven module which will not work. – khmarbaise Jun 03 '17 at 20:00
  • Following the recommendation is also possible with Maven. With Maven and also with IntelliJ IDEA this project directory layout works fine. So I think it should also work in Eclipse. For the sake of completeness I tried the project directory layout proposed from you (removing the line: `src/main/java/a...` in POM, and files in: `proj2\a\src\main\java\module-info.java` and `proj2\a\src\main\java\a\App.java`. The result is the same with same errors: It does not work in Eclipse. – Torsten Horn Jun 03 '17 at 20:46
  • Where is your pom file located? I think in `proj2/pom.xml`? If yes then move your folders to `proj2/src/main/java` ...etc. Also for the others..otherwise you are not following the maven conventions... – khmarbaise Jun 03 '17 at 20:59
  • My Maven project layout follows the Maven conventions for "Multiple Module Projects" in a "Hierachical project layout": As shown above under "Create a new Jigsaw-Maven-Project" I have two POMs: One parent POM in `proj2` and one module POM in `proj2\a`. The more important POM is the last one in the `proj2\a` directory, where `src/main/java/...` is located. – Torsten Horn Jun 03 '17 at 21:20
  • Can you please put that example project on github so we have something more concrete to talk about.... – khmarbaise Jun 04 '17 at 09:20
  • I simpified the project. The new project `proj-a` has only one POM and shows the same errors. You can download it together with config file and screenshot from: https://bugs.eclipse.org/bugs/attachment.cgi?id=268742 – Torsten Horn Jun 04 '17 at 10:44

2 Answers2

2

This is a bug in Eclipse Oxygen 4.7 RC3, see:
Eclipse Bug 517777: Running a Java 9 application in Eclipse Oxygen 4.7 does not set the module path (https://bugs.eclipse.org/bugs/show_bug.cgi?id=517777)
and
Eclipse Bug 514760: Run configuration should support notion of modules (https://bugs.eclipse.org/bugs/show_bug.cgi?id=514760)

0

Typically, all the classes within a single 'src' folder should be enclosed and packaged in a single module-info.java Also, the module-info.java should be immediately within src package. For you, the classes within the directory proj1\a\src\ should be combined. And you should maintain the module-info.java at the same level. Follow this blog for complete deployment.

Akash Mishra
  • 682
  • 1
  • 5
  • 13