5

I tried java 9 with eclipse oxygen 1a Release. As far as Simple java 9 project is concern. It is working. Here is the screen shot of java 9 simple program

Java 9 Without Maven in eclipse

Here is my Welcome.java. It can be seen that I am using Java 9 feature in this program

package com.jdojo.intro;

public class Welcome {

    public static void main(String[] args) {

        System.out.println("Welcome to the Module System.");

        // Print the module name of the Welcome class
        Class<Welcome> cls = Welcome.class;
        Module mod = cls.getModule();
        String moduleName = mod.getName();
        System.out.format("Module Name: %s%n", moduleName);
    }
}

But with Maven project with Java 9 I am having problem. I created almost same project with maven. Here is my pom.xml file. I also used junit 5 dependency

<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>pk.training.basit</groupId>
    <artifactId>Ch3-02-HelloWorldMaven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Ch3-02-HelloWorldMaven</name>
    <url>http://maven.apache.org</url>

    <properties>

        <java-version>9</java-version>

        <!-- Unit testing -->
        <junit.version>4.12</junit.version>
        <junit-jupiter-engine.version>5.0.1</junit-jupiter-engine.version>
        <junit-platform-runner.version>1.0.1</junit-platform-runner.version>

        <!-- Logging -->
        <log4j.version>2.9.1</log4j.version>
        <jboss-logging.version>3.3.1.Final</jboss-logging.version>

        <junit-jupiter-engine.version>5.0.1</junit-jupiter-engine.version> 

        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
        <maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
        <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
        <maven-failsafe-plugin.version>2.20</maven-failsafe-plugin.version>
        <maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

    <dependencies>

        <!-- Unit Testing  -->

        <!-- JUnit Jupiter test engine implementation, only required at runtime. -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter-engine.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit-platform-runner.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Logging-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
            <scope>compile</scope>
        </dependency>
        ......
</dependencies>

<build>
    <pluginManagement>
        <plugins>

            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-enforcer-plugin</artifactId>
                                    <versionRange>[1.0.0,)</versionRange>
                                    <goals>
                                        <goal>enforce</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>${exec-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>pk.training.basit.HelloFXApp</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>${maven-javadoc-plugin.version}</version>
            <configuration>
                <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
        </plugin>
    </plugins>
</build>

The project structure can be seen in the screen shot. Here is my AppTest.java

package pk.training.basit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class AppTest {

    @Test
    void myFirstTest() {
        assertEquals(2, 1 + 1);
    }
}

If module-info.java file is not present in src/main/java. Then this test case run. By simply mouse-right click and select Run As--> Junit Test. If I select the java file, which is Welcome.java and select Run As --> Java Application. Then I get the following output

Welcome to the Module System.
Module Name: null

Which is OK. Because now it is considering class path. And no module is present on module path. So module name is null. But as soon as I introduce module-info.java in src/main/java/ folder. Things start getting messy. Here what I tried with module-info.java. First I tried this

module pk.training.basit {

}

Same as my First project without maven. I get error in my AppTest.java. Imports are not resolving. Now if I right click on my Welcome.java and select Run As --> Java Application . It says error exist want to proceed. If I select yes. I get the following output

Error occurred during initialization of boot layer
java.lang.module.FindException: Module pk.training.basit not found

Now if I delete my module-info.java. And then right mouse click on my project name which is Ch3-02-HelloWorldMaven and select Configure --> Create module-info.java. It asks me for module name. If I enter my module name pk.training.basit and click Finish. Then here is the result

Java 9 Maven error

Now if i again try to run the Welcome.java. I get the error

Error occurred during initialization of boot layer
java.lang.module.FindException: Module pk.training.basit not found

Now if i remove my AppTest.java. And change module-info.java to this

module pk.training.basit {
    exports pk.training.basit;
    //requires org.junit.jupiter.api;
}

And try to run the Welcome.java. Although there is no error present now in project. I get the output

Error occurred during initialization of boot layer
java.lang.module.FindException: Module pk.training.basit not found

I don't know what I am doing wrong. Or there is problem with maven or eclipse. But here what I tried with eclipse oxygen 1a, Maven and eclipse.

Also I just have java 9 GA. No java 8 or java 7 on my machine. For just java 9 I have to include this in eclipse.ini file. -vm C:\Program Files\Java\jdk-9\bin\javaw.exe

....
openFile
--launcher.appendVmargs
-vm
C:\Program Files\Java\jdk-9\bin\javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.8
-Dosgi.instance.area.default=@user.home/eclipse-workspace
-XX:+UseG1GC
....

If I am doing something wrong. Then please let me know.

Also you can't have more than one module-info.java file in one project in eclipse. Like if we consider this link Java 9 multi module project. Then for multi module project the structure is you have project root folder. Then in project root folder you have one source folder for one module. And another source folder for another module. Both source folders have their own module-info.java file.

But if I try to do the same in eclipse. Then I get the error. Multiple modules-info.java files found on class path.

Like the idea is you have one maven project. Which manage all your dependencies. Like spring, apache, etc. And in this project you have one source folder like module.web which contains your web modules related java files and its own module-info.java. You have another source folder module.service. which contains its own module-info.java.

But it seems you can't do it in eclipse in single project. I don't know it's valid or not but I tried it and I see you can't do it in eclipse in this way.

Anyways if I am doing something wrong then please let me know.

Thank you.

Basit
  • 8,426
  • 46
  • 116
  • 196
  • Actually, m2e is developed separately. And you can say maven with eclipse is not yet ready for java 9. Though you can develop without using maven,just adding libraries to classpath and creating a module. – Amit Kumar Oct 21 '17 at 09:27
  • 2
    See [Eclipse bug 525948](https://bugs.eclipse.org/bugs/show_bug.cgi?id=525948) (which will be fixed in Oxygen.2 on December 20, 2017) – howlger Oct 21 '17 at 14:16
  • I had the same issue with a simple hello world example. and I sort of fixed it by using the vm arguments option in the run configuration. See this question https://stackoverflow.com/questions/47511379/eclipse-module-not-found-when-adding-module-info-java/47512521#47512521 – Gert Kommer Nov 27 '17 at 13:53

0 Answers0