The Java Cafe

The Java Cafe is a community of 3,665 amazing users

Java and Cloud content, by the community for the community

Create new account Log in
loading...

Launch Single-File Source-Code Programs

billykorando profile image Billy Korando ☕️ #BLM ・2 min read

Launch Single-File Source Code Programs, added in JDK 11 (JEP 330), allows a single-file Java source-code program to be directly launched by the java launcher.

Requirements for to Launch a Source-Code file

When using the java launcher to launch a source-code file program, the launcher will check for the first top-level class defined in the source-code file, in the example below SingleFileI, and that that class contains a public static void main(String[] args) method. If those are present the launcher will attempt to compile and execute the program.

public class SingleFileI {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Single File, Multiple Classes

In my cases Java developers follow a one class to one source-file idiom. This is good for readability, but not a requirement in Java. A Java source-file can contain multiple top level classes, though only one can be public, as well inner classes, classes defined within another class.

Multiple classes can allow for encapsulation of logic and behavior and defining of data carrier(s), for data processing purposes.

The below code when executed:

public class SingleFileII {
    public static void main(String[] args) {
        AnotherClass anotherClass = new AnotherClass();
        YetAnotherClass yetAnotherClass = new YetAnotherClass();

        System.out.println(anotherClass.printAnotherMessage());
        System.out.println(yetAnotherClass.printYetAnotherMessage());
    }
}

class AnotherClass{
    public String printAnotherMessage() {
        return "anotherMessage";
    }
}

class YetAnotherClass{
    public String printYetAnotherMessage() {
        return "yetAnotherMessage";
    }
}
Enter fullscreen mode Exit fullscreen mode

Prints out the following:

anotherMessage
yetAnotherMessage
Enter fullscreen mode Exit fullscreen mode

Referencing non-JDK Classes

Classes that are not in the core-JDK can also be referenced in a single-file source code application, like in the example below using the RandomUtils located i nthe commons-lang3 library, however a classpath argument (-cp or -classpath) to the class or library must be provided.

In the below example, to successfully launch the application the command would look something like this:

java -cp /path/to/commons-lang3-3.12.0.jar SingleFileIII.jar
Enter fullscreen mode Exit fullscreen mode
import org.apache.commons.lang3.RandomUtils;

public class SingleFileIII {

    public static void main(String[] args) {
        System.out.println(RandomUtils.nextInt());
    }

}
Enter fullscreen mode Exit fullscreen mode

Note: This behavior is consistent with how a normally compiled and executed Java program would work, but as most Java developers typically use build tools and frameworks, this behavior is often hidden.

Discussion (0)

pic
Editor guide