New candidate JEP: 445: Flexible Main Methods and Anonymous Main Classes, Preview

Nathan Walker nathan.h.walker at gmail.com
Tue Apr 11 01:53:44 UTC 2023


After reading the JEP, I am a bit unclear on how to launch a pre-compiled
anonymous class, or if that will even be permitted.

The JEP gives this example:

java HelloWorld.java


Makes complete sense to me.  But then it goes on to say:

In this case the compiler chooses HelloWorld for the class name as an
> implementation detail, but that name still cannot be used directly in Java
> source code.


It also says in an earlier part

 An anonymous main class cannot be referenced by name


So, if I package up my anonymous class for deployment in a jar, can I
launch it with "java -cp myjar.jar HelloWorld"?  Or is the HelloWorld name
an implementation detail that can not be relied on here?  My first
impression on reading this JEP was that it was limited to source file
programs like in the example (which would be a shame).

But with further reflection on this earlier statement that didn't seem
correct:

An anonymous main class resides in the unnamed package, and the unnamed
> package resides in the unnamed module. While there can be only one unnamed
> package (barring multiple class loaders) and only one unnamed module,
> ***there can be multiple anonymous main classes in the unnamed module***.
> Every anonymous main class contains a main method and so represents a
> program, thus multiple such classes in the unnamed package represent
> multiple programs.


I am not sure what use multiple anonymous main classes would have if we
can't target which one we want to launch, so I feel like I am missing
something here.

Thanks for your time,
Nathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230410/7ae47575/attachment-0001.htm>


More information about the amber-dev mailing list