directory layout for Java 9 modules

Viewed 2501

I've been trying to write a Java 9 module, but no matter what I do, I get the error "package is empty or does not exist". I've tried searching online for the expected directory layout, and tried every variation on directory layout I can think of, but nothing works.

For example, here is one layout I tried

bar.foo/module-info.java

module bar.foo {
    exports bar.foo;
}

bar.foo/bar/foo/wtf.java

package bar.foo;

public class wtf {
}

However, compilation still gives the same error as usual.

> javac bar.foo/module-info.java 
bar.foo/module-info.java:2: error: package is empty or does not exist: bar.foo
    exports bar.foo;
               ^
1 error
2 Answers

The command instead that should work is :

javac bar.foo/module-info.java bar.foo/bar/foo/wtf.java

The reason being, you're trying to compile a class which requires a package to exist i.e. bar.foo in your case. But since you haven't created one, the compiler throws the mentioned error.

It's not about the directory layout but the .class evaluated by the compiler. Providing your other class(bar.foo/bar/foo/wtf.java) creates the wtf.class within that package and hence the compiler would succeedingly compile the module-info.class as well.

I ran into the same issue when trying to add a module-info.java to a JAR file for which I didn't have source. I had to add an empty class bar.foo.Bogus and compile with

javac bar.foo/module-info.java bar.foo/bar/foo/Bogus.java

You can remove the bogus class afterwards.

Related