Core Java

JDK 11: Taking Single-File Java Source-Code Programs Out for a Spin

The JDK 11 Early Access Builds include preview 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.

single file java source code

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):

single file java source code

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”):

single file java source code

This error occurs because the JEP 330 support in this early access build is a “preview” feature. The following screen snapshot demonstrates that it works when I pass the flags --source=11 and --enable-preview to the Java launcher.

single file java source code

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.

single file source code

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 single source code file helloYou.java (even with the .java extension and without the --enable-preview flag).

single file source code

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”.

This post has been a first look at the preview feature single-file source-code programs that is now available in the JDK 11 Early Access Builds.

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: JDK 11: Taking Single-File Java Source-Code Programs Out for a Spin

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button