JEP 458: Launch Multi-File Source-Code Program: Inferring the root of the source tree

Ron Pressler ron.pressler at oracle.com
Thu Oct 19 21:47:14 UTC 2023


JEP 458: Launch Multi-File Source-Code Programs (https://openjdk.org/jeps/458) describes a mechanism by which the root of a source directory is computed (for the purpose of locating other referenced classes) from the path of the launched file and its directory; that root is then used implicitly as if `--source-path <root>` has been passed to javac. The algorithm for inferring the source compares the .java file’s package against its filesystem path and “subtracts” the package from the suffix of the filesystem path.

For example, if in directory dir/a/b/c I have the file C.java and I launch it as a program with `java dir/a/b/c/C.java` then:

- If C.java declares its package to be a.b.c then the root of the source tree is inferred to be dir
- If C.java declares its package to be b.c then the root of the source tree is inferred to be dir/a
- If C.java declares its package to be c then the root of the source tree is inferred to be dir/a/b
- If C.java doesn’t declare a package (i.e. it’s in the unnamed package), then the root of the source tree is inferred to be dir/a/b/c
- If C.java declares its package to be p (which doesn’t match any suffix of its path), then we get an error.

The current working directory from which we invoke `java` is ignored.

This is different from the way javac behaves. If we run `javac dir/a/b/c/C.java`, then, for the purpose of resolving other referenced classes, the root of the tree is always the current working directory, and the package that C.java declares is ignored (it can be p and the compilation will succeed).

The algorithm for inferring the root of the source tree in the source-code launcher was chosen so that programs could be easily launched directly from source from any working directory without explicitly setting a source tree root, under the reasonable assumption that source files are placed as we would expect in a project. I would like to get some feedback about the algorithm’s suitability and reasonableness.

— Ron




More information about the compiler-dev mailing list