JEP proposed to target JDK 11: 330: Launch Single-File Source-Code Programs
stuart.monteith at linaro.org
Thu May 24 13:56:24 UTC 2018
While we are talking about platform specifics, I notice nobody has
mentioned binfmt_misc. If you are running on a suitably configured
Linux system you could just type:
echo ":java:E::java::<your JAVA_HOME>/bin/java:"
That will allow you to run:
without modify the source at all.
Using binfmt_misc to handle "//! " at the beginning is possible with
some magic bytes and a wrapper are also possible if you want to avoid
needing the .java extension.
Most Debian installations will have an entry for "jar" files already.
While there probably isn't a scheme that would allow a single .java
file to run from the command line (or icon) on every platform, there
are perhaps platform specific means on most platforms.
On 24 May 2018 at 08:23, Robert Zenz <robert.zenz at sibvisions.com> wrote:
> I'm sorry, I still don't get why the shebang line is required at all for this
> proposal. I might argue that shebang lines are system dependent features and are
> typically only used with languages which treat `#` as a comment which is not the
> case in Java. Additionally, we can already "emulate" shebangs in Java (or any C
> like language) source files using a polyglot. Which is fore sure not that easy
> to understand, but it still gets the job done.
> I know what you're trying to do, but I fail to see how the achieved goal (of
> easily executing a Java source file by doing `./HelloWorld.java` versus `java
> HelloWorld.java`) validates the added special case just to support the shebang
> line (including possible confusion of newcomers why the first line is allowed to
> start with `#` but everywhere else is a syntax error).
> On 23.05.2018 23:01, Jonathan Gibbons wrote:
>> On 5/17/18 1:12 PM, mark.reinhold at oracle.com wrote:
>>> The following JEP is proposed to target JDK 11:
>>> 330: Launch Single-File Source-Code Programs
>>> Feedback on this proposal is more than welcome, as are reasoned
>>> objections. If no such objections are raised by 23:00 UTC on Thursday,
>>> 24 May, or if they're raised and then satisfactorily answered, then
>>> per the JEP 2.0 process proposal  I'll target this JEP to JDK 11.
>>> - Mark
>>>  http://cr.openjdk.java.net/~mr/jep/jep-2.0-02.html
>> A number of points have been raised, regarding the interaction with javac and
>> shebang scripts.
>> It was never a goal to modify javac to support shebang-java scripts. This can be
>> seen indirectly in the JEP in the description of how the first line may be
>> removed before passing the rest of the file as a normal CompilationUnit to the
>> compiler. With hindsight, this deserves to be stated as an explicit Non-Goal.
>> There are various reasons to not want to change JLS or javac:
>> 1. Changing JLS is a Big Deal, and comes with its own costs and constraints.
>> Further, shebang files are a platform-specific feature, and are not even defined
>> in the POSIX standard. The feature does not warrant changing a JCP-controlled
>> 2. Changing javac to accept shebang-java files is also a Big Deal. Modifying the
>> command-line options to accept files that do not follow the standard naming
>> conventions would introduce complexity and potential ambiguity.
>> 3. There is no compelling need to change JLS or javac. As demonstrated by the
>> proposed implementation, no change to JLS or javac is actually necessary in
>> order to implement the feature. It is therefore at most a convenience if javac
>> were to be adapted to ignore shebang lines.
>> Shebang scripts are an executable format defined on some, but not all,
>> platforms. Creating a shebang script is typically more than just adding an
>> initial first line to a file; it typically involves a number of steps:
>> a. Add an initial shebang line to the file
>> b. Rename the file to a "command-friendly" name
>> c. Make the file executable
>> d. Install the file in some standard location
>> While renaming the file to a command-friendly name is optional, it is also
>> expected to be common practice. For example, a source file named
>> `HelloWorld.java` might be installed as `helloworld`. And, while the JEP
>> describes use cases for executing a small single-file program with `java
>> HelloWorld.java` or executing it as a platform-specific shebang script with just
>> `helloworld`, it does not seem like there is a common use case to execute
>> `HelloWorld.java`. So, if the shebang script is typically renamed to a
>> command-friendly name, it will not be possible to compile it directly, with
>> "javac helloworld", because that is not a valid command line for javac. This
>> reduces any potential convenience of having javac ignore shebang lines.
>> Since Java source files are different artifacts to platform-specific executable
>> scripts, it makes sense to treat them differently, and since we do not want to
>> change the Java language to support shebang lines, the suggestion is to amend
>> the JEP and implementation so that shebang lines are never stripped from Java
>> source files, i.e. files ending in `.java`. This avoids the problem of having
>> the ecosystem of tools handling Java source files having to deal with arbitrary
>> artifacts like shebang lines. The change would still permit the direct
>> execution of Java source files, such as `java HelloWorld.java`, and the
>> execution of shebang scripts, such as `helloworld`.
>> -- Jon
More information about the jdk-dev