0% found this document useful (0 votes)
6 views

Launching Single -File Source-Code Programs

Uploaded by

patrickmole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Launching Single -File Source-Code Programs

Uploaded by

patrickmole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Launching Single-File Source-Code Programs

Single-File Source-Code Program Execution

In JDK 11, Java introduced the ability to launch a single-file source-code program with the java launcher,
without first needing to explicitly compile the source code. This works by the java launcher automatically
invoking the compiler and storing the compiled code in-memory. This can be a great way to learn how to
use Java or explore new features within the Java API, without having to go through the cruft of compiling
and then executing code. There are several ways to use this feature, as well as some limitations and
things to keep in mind.

Executing Your First Single-File Source-Code Program

To execute a single-file source-code program, the first class defined in the source file must contain public
static void main(String[]) like in HelloWorld below:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

Copy

From the command line, HelloWorld can be launched with (accepting the name of the file is
also HelloWorld.java):

$ java HelloWorld.java

Copy

Passing in Arguments

Arguments can also be passed in like with a normally compiled class, so in the below:

public class HelloJava {

public static void main(String[] args) {

System.out.println("Hello " + args[0]);


}

Copy

Passing in an argument can be done like this:

$ java HelloJava.java World!

Copy

Multiple Classes in Same File

Multiple classes can be defined within the same source file if needed for encapsulation purposes, like in
this example:

public class MultipleClassesInSameFile {

public static void main(String[] args) {

System.out.println(GenerateMessage.generateMessage());

System.out.println(AnotherMessage.generateAnotherMessage());

class GenerateMessage {

static String generateMessage() {

return "Here is one message";

class AnotherMessage {

static String generateAnotherMessage() {

return "Here is another message";

}
}

Copy

When executed:

$ java MultipleClassesInSameFile.java

Copy

Will output:

Here is one message

Here is another message

Copy

Reference JDK Classes and Non-JDK Classes

A class that is part of the core JDK does not need to be added to the classpath to be executed. So this
example, referencing the Scanner and MatchResult classes, can be executed simply with
the java launcher:

import java.util.Scanner;

import java.util.regex.MatchResult;

public class ScannerExample {

public static void main(String... args) {

String wordsAndNumbers = """

Longing rusted furnace

daybreak 17 benign

9 homecoming 1

freight car

""";

try (Scanner scanner = new Scanner(wordsAndNumbers)) {

scanner.findAll("benign").map(MatchResult::group).forEach(System.out::println);
}

Copy

To launch:

$ java ScannerExample.java

Copy

However the below example referencing RandomUtils, part of the Apache Commons Lang, will need to
have the commons-lang.jar added to the classpath at launch:

import org.apache.commons.lang3.RandomUtils;

public class ReferenceNonJDKClass {

public static void main(String[] args) {

System.out.println(RandomUtils.nextInt());

Copy

To launch:

java -cp /path/to/commons-lang3-3.12.0.jar ReferenceNonJDKClass.java

Copy

Executing as a Shebang File

On a Unix-like operating system, a single-file source-code application, can also be launched as a shebang
file like a script. Within the a java source file, as the first line in the file add path/to/java/home --source
<version> like in the below example:

#!/path/to/your/bin/java --source 23

public class HelloJava {


public static void main(String[] args) {

System.out.println("Hello " + args[0]);

Copy

The file cannot have .java as its file extension, and must also be executable chmod +x. With that, it can
be launched with:

./HelloJava

You might also like