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

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Oct 19 23:37:43 UTC 2023


The algorithm you describe seems reasonable, at least from the 
perspective of the developer invoking the java launcher.

It does add some complexity because now you need to first be able to 
parse the sourcefile in question in the first place in order to be able 
to determine what the source path of the compilation should be, so 
effectively you end up with a chicken and egg problem. It's not a big 
issue for the launcher - given that the launcher is built on top of 
javac. I see that the launcher code spins a quick parsing of the source, 
in a separate java compilation environment, just for this purpose:

https://github.com/openjdk/jdk/blob/093ab1cef7383323deb74bffa511b81109ca4dd6/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/ProgramDescriptor.java#L53

I think that's ok. Also, the logic for "subtracting" the package part 
from the path doesn't seem too daunting, and is made relatively robust 
and portable by the use of Paths:

https://github.com/openjdk/jdk/blob/093ab1cef7383323deb74bffa511b81109ca4dd6/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/ProgramDescriptor.java#L73

That said, if you are wondering whether the behavior of javac could 
eventually be aligned to match this, I'd be more skeptical, as the 
chicken and egg problem would become more manifest I think, as you'd 
need to spin up a java compiler while launching another... while we 
might find some workarouns in this direction, I'm not sure it'd be worth 
it, and it will also be a biggie behavioral compatibility issue (for javac).

Summing up, I think your approach is workable. I don't know exactly (as 
I had not have the time to play around with the new launcher much) how 
much this algorithm _exactly_ brings on the table. Using the current 
folder as the source path is not a completely terrible default, and has 
the enormous advantage of not requiring any parsing step, so it's a very 
pragmatical one from a tooling perspective. But ultimately, I believe 
that, in the context of a JEP like this, usability has to take the front 
seat. I imagine there are some examples and use cases where the proposed 
algorithm worked and the "classic" one didn't?

P.S.
I suppose the plan is to only support classpath applications only - 
otherwise there's also a module sourcepath to infer, and that could be a 
bit trickier :-)

Cheers
Maurizio


On 19/10/2023 22:47, Ron Pressler wrote:
> 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.


More information about the compiler-dev mailing list