How do I create a simple module in Java?

How do I create a simple module in Java?

Problem Description:

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 – 1

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
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject