# Io / Github / Biezhi / Java11 / Singlefile

## Overview

The `io.github.biezhi.java11.singlefile` package is a demonstration module for [JEP 330](http://openjdk.java.net/jeps/330) — Java's single-file source-mode execution feature introduced in Java 11. It shows how a single Java source file can be compiled and executed directly via the `java` launcher without requiring a separate `javac` step or classpath setup. This package lives within a larger collection of Java 11 feature showcase projects authored by [biezhi](https://github.com/biezhi), serving as a practical entry point for engineers learning about the new single-file execution capability.

## Key Classes and Interfaces

### [HelloWorld](src/main/java/io/github/biezhi/java11/singlefile/HelloWorld.java)

The sole class in this package. `HelloWorld` is a public, top-level class whose only purpose is to demonstrate the Java 11 single-file execution model.

#### Purpose

This class exists to prove that a Java source file containing a `public static void main(String[])` method can be run directly with `java HelloWorld.java` — the launcher handles both compilation and execution transparently.

#### Source file

- [HelloWorld.java](src/main/java/io/github/biezhi/java11/singlefile/HelloWorld.java) — lines 1–23

#### Class structure

The class is minimal:

| Item | Type | Line(s) |
|---|---|---|
| `HelloWorld` (class) | `public class` | 17 |
| `main(String[] args)` | `public static void main` | 19–21 |

#### `main(String[] args)` method

| Property | Detail |
|---|---|
| **Signature** | `public static void main(String[] args)` |
| **Return type** | `void` |
| **Parameters** | `args` — standard CLI argument array (unused in this example) |
| **Side effects** | Prints `"Hello Guys, this is Java 11."` to standard output |
| **Lines** | 19–21 |

The method body is a single `System.out.println()` call. It takes no business logic, performs no branching, and does not use the `args` parameter. Its sole job is to produce console output as proof that the file executed successfully.

## How It Works

This module demonstrates **JEP 330: Compile and Run Single-File Source-Code Programs**, which was standardized for Java 11 (JDK 11, released March 2018).

### The single-file execution flow

1. You invoke `java HelloWorld.java` from a terminal.
2. The Java launcher detects that the argument points to a source file (`.java` extension).
3. Internally, the launcher compiles the source to in-memory bytecode (equivalent to `javac -d <memory> HelloWorld.java`).
4. The launcher then loads the compiled class from memory and invokes the `main` method (equivalent to `java -cp <memory> HelloWorld`).
5. The program prints its output and exits.

No `javac` binary is invoked explicitly. No `.class` files are written to disk. The entire compile-and-run cycle happens inside the JVM process.

```mermaid
flowchart TD
    Pkg["io.github.biezhi.java11.singlefile"] --> Src["HelloWorld.java"]
    Src --> Class["HelloWorld"]
    Class --> Method["main(String[] args)"]
    Method --> Output["Prints:
'Hello Guys, this is Java 11.'"]
```

### Why this matters

Before Java 11, running a single Java file required two explicit steps:

```bash
# Pre-Java 11
javac HelloWorld.java
java HelloWorld
```

With JEP 330, this collapses to one command:

```bash
# Java 11+
java HelloWorld.java
```

This is especially useful for small scripts, prototypes, and educational examples — all of which describe exactly what this module is.

## Dependencies and Integration

This module has **no external dependencies**. It imports no libraries, depends on no other packages, and produces no artifacts beyond its source file. It is entirely self-contained.

The package sits at the root level of the module hierarchy (parent: root), meaning it is a top-level example within the broader Java 11 features collection rather than a sub-component of a larger system.

## Notes for Developers

- **Java version requirement**: This module requires at least Java 11. Running it on Java 8 or earlier will fail because the single-file execution mode does not exist.

- **No classpath management**: Because the launcher handles compilation internally, you do not need to configure a classpath. However, if you need third-party libraries, you must pass them via `--class-path` or `--module-path` flags on the `java` command.

- **One public top-level class**: JEP 330 only supports source files with at most one public top-level class or interface (matching the filename). `HelloWorld` follows this rule since its class name matches the file name.

- **Extensibility**: To add a new example, follow the same pattern — create a `.java` file in this package with a `public class` matching the file name and a `main` method. The JEP 330 mechanism will handle the rest.

- **Author and origin**: The source was authored by **biezhi** and dated **2018/8/1** (shortly after Java 11's release in March 2018), making it one of the early examples of this feature in practice.
