Draft JEP: Launch Single-File Source-Code Programs

Jonathan Gibbons jonathan.gibbons at oracle.com
Mon Feb 12 16:40:21 UTC 2018

The JLS is primarily concerned about the abstract content of compilation 
units, and generally stays clear of any issues related to how and where 
compilation units are actually stored. From the earliest days, it has 
admitted the possibility that compilation units may be stored in 
"non-traditional" storage formats such as a database.  So, it is not 
fair to say that "according to the JLS [they] cannot be called Java 
source files."

In the current JLS, it goes so far as to say:

    7.2. Host Support for Modules and Packages
    Each host system determines how modules, packages, and compilation
    units are created and stored.

But even that assurance does not need to come into play here. Since JDK 
6, with the introduction of the Compiler API (javax.tools.*, JSR 199) 
the compiler has permitted a compilation unit to be provided in a 
"JavaFileObject". Using JavaFileObjects, you can provide compilation 
units to a compiler that come from traditional source files, entries in 
zip files, or from in-memory objects like Strings and more complex 
representations like the buffers for IDE editor windows.  That ability 
can come into play here: the source file launcher can create a 
JavaFileObject containing the compilation unit found in the file, and 
can pass that JavaFileObject to the compiler via the standard 
javax.tools API, with no changes required to the compiler at all.

So, I agree that the "shebang" files we are talking about are not 
regular Java source files, and cannot be passed in their entirety to a 
Java compiler. Maybe we need to come up with a more specific name for 
such files. But as far as as their content is concerned, the compilation 
unit passed by the source launcher to the compiler is a standard 
compilation unit, as defined in the JLS.

-- Jon

On 2/12/18 6:22 AM, Roger Riggs wrote:
> Hi Jonathan,
> It would be  a great convenience to developers to NOT to have two 
> types of java source files.
> Those that can be run directly and those that cannot. According to the 
> JLS, the former can not
> be called java source files, they are something else.
> $.02, Roger
> On 2/10/2018 11:59 AM, Jonathan Gibbons wrote:
>> Volker,
>> I don't see a compelling need to change the JLS to accommodate 
>> "shebang" files. Most Java source files don't need the `#!` line, 
>> including any source file passed to an explicit invocation of the 
>> Java launcher. The line is only needed when the file is set up to be 
>> directly executed by the "shebang" mechanism, in which case the line 
>> can be thought of as a "wrapper" supported by the host system to 
>> enable such use. If the feature is sufficiently popular, it may even 
>> be that editors will recognize and support the use of such lines, in 
>> the same way that some editors recognize and support the manipulation 
>> of javadoc comments, which are also not defined in the JLS (beyond 
>> the basic definition of a comment in JLS 3.7).
>> -- Jon
>> On 2/9/18 2:51 AM, Volker Simonis wrote:
>>> Hi Jonathan,
>>> that's an interesting proposal. It is just unfortunate that a Java
>>> source file starting with "shebang" won't be a legal Java file
>>> according to the Java Language Specification. Or do you plan to change
>>> the JLS to allow (and ignore) "shebang" in the first line?
>>> Thanks,
>>> Volker
>>> On Fri, Feb 9, 2018 at 12:18 AM, Jonathan Gibbons
>>> <jonathan.gibbons at oracle.com> wrote:
>>>> This draft JEP contains a proposal to enhance the java launcher to 
>>>> support
>>>> running a
>>>> program supplied as a single file of Java source code. The program 
>>>> will be
>>>> compiled
>>>> and run, without the need to explicit invoke javac, or to package 
>>>> up a jar
>>>> file.
>>>> For more details, see:
>>>> http://openjdk.java.net/jeps/8192920
>>>> -- Jon

More information about the jdk-dev mailing list