Search code examples
javajava-modulejava-17

How do I create a simple module in Java?


I have a very simple library with classes in one package. As far as I know, it only depends on things in java.base. I added a module-info.java to the top level directory that exports this package.

e.g.

module org.foo.bar {
    exports org.foo.bar.pkgname;
}

I then just built a simple jar file with my classes and the module-info class.

Is that it, or is there more that I need to do?


Solution

  • That's it, as long as your jar is organized as follows:

    module-info.class
    org
     foo
      bar
       pkgname
        X.class
        Y.class
    

    A jar with a module-info.class at top level is a module. You can check your module with:

    jar --describe-module --file bar.jar