简体   繁体   中英

Java 9 Maven module not found in console

I am building a Jigsaw module using Maven.Here is a simple class inside of it

public class Welcome {

    public static void main(String[] args) {
        System.out.println("Java 9 Modular Welcome");

        Module module =  Welcome.class.getModule();
        System.out.printf("Module name: %s%n",module.getName());
    }
}

module-info.java

module org.abondar.experimental.intro {

exports  org.abondar.experimental.intro;
}

Maven compiler plugin set up

 <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <jdkToolchain>
                        <version>[1.8,9)</version>
                    </jdkToolchain>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
    </plugins>
    </build>

And Jar plugin

  <build>
        <sourceDirectory>src/main/org.abondar.experimental.intro</sourceDirectory>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.abondar.experimental.intro.Welcome</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./lib</Class-Path>
                            <Module-Path>./mods</Module-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

        </plugins>
    </build>

So in IDE this code works as expected. But in console getModule returns null. I believe I have misconfigured one of Maven plugins because in console module is not in module path. So what exactly is wr

Alright, after sometime and careful reading I understood my mistake: I didn't run jar correctly.

So here how should it be:

java --module-path target/Intro-1.0.jar -m org.abondar.experimental.intro/org.abondar.experimental.intro.Welcome

Instead of just running

java -jar target/Intro-1.0.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM