JDK-8320259 : Add method to java.lang.Class to find main method
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Priority: P4
  • Status: Closed
  • Resolution: Withdrawn
  • Fix Versions: 25
  • Submitted: 2023-11-16
  • Updated: 2025-05-16
  • Resolved: 2025-05-16
Related Reports
CSR :  
Description
Summary
-------

Introduce a new method that returns the main method to invoke per JLS of the given presumably initial class or interface.

Problem
-------

With the introduction of JEP 512, Compact Source Files and Instance Main Methods, the determination of an initial class or interface's main method to be invoked is no longer a simple algorithm. There are now many candidate methods.

Solution
--------

Introduce a new method, `getMainClass`, in `java.lang.Class` that returns the `main` method to invoke. This is helpful for when a class has many candidate methods.

This new method does not involve determining if a class or interface is the initial class or interface, initialization of that class or interface, or how the `main` method should be invoked, such as pushing arguments or initializing an object instance.

Specification
-------------

```
    /**
     * Return the {@code main} method to be invoked per JLS {@jls 12.1.4} when
     * this {@code Class} object represents the initial class or interface, or
     * {@code null} if no candidate method exists.
     * <p>
     * A candidate method:
     * <ul>
     * <li>has a single formal parameter of type {@code String[]}, or has no
     * formal parameter</li>
     * <li>has a return type of {@code void}</li>
     * <li>is public, protected or package private</li>
     * </ul>
     * Multiple candidate methods may be present in a class or interface.  In
     * that case, this method returns the one selected per JLS.
     *
     * @apiNote
     * It is the caller's responsibility to determine whether this {@code Class}
     * object represents the initial class or interface, initialize this class
     * or interface before invocation, and find out how to invoke the returned
     * {@code main} method.
     *
     * @return the main method to be invoked, may be {@code null}
     *
     * @jls 8.2 Class Members
     * @jls 8.4 Method Declarations
     * @jls 8.4.2 Method Signature
     * @jls 12.1.4 Invoke a {@code main} method
     * @since 25
     */
```
Comments
Hearing objections as to this provides too little in an "alternative main" mechanism - that it does not handle arguments and initialization - and given those steps are better done by the launcher themselves to avoid confusing stack trace, I think it is best to withdraw this CSR and reject this proposal.
16-05-2025

Moving to Provisional. Initial round of feedback: [~liach], please add the intended signature line of the new method; I assume it is something like `public Method getMainClass()` Please mention varargs methods are okay too.
15-05-2025

Proposing for initial review. In particular, per previous feedback, I wish for scrutiny against the API notes that describes what this API is NOT. During development I found some inconsistencies in the implementation of JEP 512; those issues may be their own tickets and may or may not be included in this CSR.
14-05-2025

I'm a bit uncomfortable with this proposal. I agree that it might be useful to expose an API to get the main method but it's to clear that java.lang.Class is the right place, it looks out of place for Class to define a method to help launchers search the class hierarchy. If there is a standard API for finding the main method then it begs the question as to where the API is to find the main class or the main module. So I think we need to think a bit more about this and maybe it could be separated from JEP 463.
21-11-2023

(For the above comment, the preview looks fine, but the final version did not preserve the formatting.)
20-11-2023

[~jlaskey], for > ...have a single argument of type {@code String[]}... Please add a clarification that `String...` is also acceptable; something like > have a single argument of type {@code String[]}/{@code String...} or semantically equivalent alternative phrasing.
20-11-2023

[~jlaskey], it would be helpful to cite that restriction in the note, including a `@jvms` and/or `@jls` citation if one is relevant.
17-11-2023

It's not possible to have an instance and static method with the same signature.
17-11-2023

Can a classfile to have both static and instance `main` method (when created by classfile library)? If so, need to state what is returned.
17-11-2023

apiNote added
17-11-2023

Moving to Provisional, not Approved. [~mchung], please reivew this CSR. [~jlaskey], can a default method of an interface be returned by Class.getMainMethod? If so, this would be good to state explicitly as an apiNote.
17-11-2023