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

本小妞迷上赌 提交于 2019-12-11 02:25:19

问题


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?


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/44343741/import-of-a-java-9-jigsaw-maven-project-in-eclipse-oxygen-4-7-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!