Draft JEP: Launch Single-File Source-Code Programs

Jonathan Gibbons jonathan.gibbons at oracle.com
Fri Feb 9 18:19:45 UTC 2018


You misunderstand the operation of the `#!` mechanism [1].  This is a 
general feature provided on Unix-derived systems, including Linux and 
MacOS. It provides a way of executing many different types of files, 
both binary files and text files, based on the first two bytes of a 
file's content, also known as its "magic number". [2]  The characters 
`#!` happen to a human-readable representation of a magic number which 
defines a way to determine a program to process the rest of the file. In 
the context of the JEP, the program to process the rest of the file 
should be the Java launcher.

Although the feature is provided by the operating system (not the Java 
launcher) it is mentioned in the context of this JEP because we need to 
take special action to ignore the contents of the first line, because it 
is not valid Java source code. This is different from some other 
programming and scripting languages, in which `#` is a character that 
introduces a comment, meaning that the rest of the line will be ignored 
anyway by the existing rules for that language.

I would emphasize that the `#!` line is only needed when you want to use 
the shebang mechanism to directly execute the source file, without even 
specifying the Java launcher on the command line. You do not need to 
provide a `#!` line in any situation in which you invoke the Java 
launcher and pass it a source file, such as this:

     java HelloWorld.java

I will update the JEP to clarify these points.

-- Jon

[1] https://en.wikipedia.org/wiki/Shebang_(Unix)
[2] https://en.wikipedia.org/wiki/Magic_number_(programming)

On 2/9/18 3:13 AM, Robert Zenz wrote:
> I fail to see why a shebang would be required at all for this proposal, can't
> `java` figure out that it was handed a source code file (ends with .java and is
> not a class file or jar)? Or alternately provide a switch to run source files?
> On 09.02.2018 11:51, 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