JDK 11: Taking Single-File Java Source-Code Programs Out for a Spin
Are you ready for JDK 11? Check out this post to take a look at how one of JDK 11's new features behaves in the wild!
Join the DZone community and get the full member experience.
Join For FreeThe 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 JEP 330 is included with this JDK distribution, which 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)
orjava [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 the single-file source programs used with this JEP 330 support was not going to be allowed to end with the .java
extension (which this 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 --source=11
flag).
Had I attempted that last maneuver with OpenJDK 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 are now available in the JDK 11 Early Access Builds.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments