The JDK 11 Early Access Builds include functionality related to JEP 330 ("Launch Single-File Source-Code Programs"). I have written about JEP 330 before in posts "Shebang Coming to Java?" and "JEP 329 and JEP 330 Proposed for JDK 11". I get to take this feature out for a spin in this post thanks to the JDK 11 Early Access Builds.
For this demonstration, I'm using the latest (as of this writing) OpenJDK JDK 11 Early Access Build 24.
One of the first indications that support for JEP 330 is included with this JDK distribution is seen when using the -help
flag (java -help
):
As shown in the last image, the "help" starts with a "usage" statement and the last example in the usage statement describes how to use the Java launcher (java) to run single-file source-code programs. Specifically, the output shows the following "usage" with the usage that is the subject of this post highlighted here:
Usage: java [options] <mainclass> [args...] (to execute a class) or java [options] -jar <jarfile> [args...] (to execute a jar file) or java [options] -m <module>[/<mainclass>] [args...] java [options] --module <module>[/<mainclass>] [args...] (to execute the main class in a module) or java [options] <sourcefile> [args] (to execute a single source-file program)
To demonstrate this feature, I'm going to use a simple example adapted (very slightly) from that provided in the 24 May 2018 Mario Torre post on the OpenJDK jdk-dev mailing list.
helloYou.jv
#!/bin/java public class Hello { public static void main(final String[] args) { final String name = System.console().readLine("\nPlease enter your name: "); System.console().printf("Hello, %s!%n", name); } }
I have called this file helloYou.jv
. Note that it does NOT end with the .java
extension that regular Java source code files end with and I did not match the name of the file to the name of the class. In fact, I started the file's name with a lower case letter!
When I try to run this file directly with OpenJDK 11 EA-24, I see an error ("Could not find or load main class helloYou.jv"):
The following screen snapshot demonstrates that it works when I pass the flag --source=11
to the Java launcher.
I highlighted in my post "Shebang Coming to Java?" that it sounded like single-file source programs used with this JEP 330 support were not going to be allowed to end with the .java
extension (which extension would be reserved for traditional Java source files). This seems to be the case as shown in the next screen snapshot where I attempt to run this feature against the same code as above, but now with the file name helloYou.java
.
The last image demonstrates that we cannot run .java
files with a shebang because they are treated as regular Java files and thus must meet the specification of regular Java source code files.
With this early access build, if I comment out the shebang line, I can run the now traditionally compliant Java single source code file helloYou.java
(even with the .java
extension and without the flag --source=11
).
Had I attempted that last maneuver with OpenJDK JDK 10, attempting to run a Java source code file like that just shown would produce the error message discussed earlier: "Error: Could not find or load main class helloYou.java". I would have needed to compile the Java source code file into a .class
file and would have needed to make sure it was on the classpath of the Java launcher.
This post has been a first look at the feature single-file source-code programs that is now available in the JDK 11 Early Access Builds.
4 comments:
This article is incorrect -- the single-file source launcher in JDK 11 is NOT a preview feature. If it was, JEP 330 would loudly proclaim the fact. It's really unclear why --enable-preview has entered the picture. All JEP 330 says is this: "If the file does not have the .java extension, the --source option must be used to force source-file mode."
Thanks Alex,
I have removed references to "preview" and --enable-preview from this post.
Dustin
Thanks Dustin. Glad you have played with the feature. Just one thing: the screenshot demonstrating that "we cannot run .java files with a shebang" still includes --enable-preview.
Thanks again Alex,
I have replaced that particular screen snapshot with one that does not include the unnecessary use of --enable-preview.
Post a Comment