Execute Java Single-File Programs
Execute Java Single-File Programs
When executing programs with multiple classes within a single file using the Java launcher, all classes are compiled in-memory during execution. However, only the class with the public static void main method is the entry point. Encapsulation here ensures that supporting classes work exclusively within the scope of this main class, preventing them from being part of the broader accessible API. As such, encapsulation leverages in-file organization and structural clarity without necessitating multiple files .
In Java's single-file source-code execution, core JDK classes can be used directly without specifying a classpath, due to their inherent integration within the Java runtime. For example, classes such as Scanner can be directly utilized within such single-file programs. However, third-party library classes, like those from Apache Commons Lang, require their respective JAR files to be explicitly included in the classpath during execution. This difference emphasizes the self-contained nature of core JDK classes versus the dependency management necessary for third-party libraries .
The single-file execution feature in JDK 11 allows Java developers to explore new Java APIs interactively without setting up complex build environments or manually handling compilation. This contrasts with traditional methods that required developers to set up IDE projects or command line scripts for compiling and running each exploration step. Single-file execution significantly reduces setup time and encourages a more exploratory and iterative approach to learning APIs, allowing developers to experiment with API features on-the-fly through direct code input .
Java's single-file source-code execution feature allows developers to run Java programs without explicitly compiling them first by using the java launcher, which compiles the source code in-memory. This capability simplifies the learning process by removing the need for separate compilation steps, making it easier to quickly test and explore Java code and features interactively .
In JDK 11's single-file source-code feature, multiple classes can be defined within the same source file for encapsulation purposes. These additional classes can support the main class by encapsulating certain functionalities separately, although they are not public and can't be directly accessed from outside packages. This approach allows developers to manage related classes conveniently in one file without affecting module encapsulation negatively, offering localized encapsulation rather than modular .
To utilize external libraries like Apache Commons Lang in a single-file source-code approach, you must include the library's JAR file in the classpath during execution. For example, if using RandomUtils from Apache Commons Lang, the command should be: java -cp /path/to/commons-lang3-3.12.0.jar ReferenceNonJDKClass.java. This ensures that the classpath includes the necessary classes in the external library for execution, as single-file execution does not automatically resolve dependencies outside the JDK .
To execute a Java program using the single-file source-code execution feature with arguments, you first write the Java class with a main method. For instance, a class named HelloJava can be run from the command line with arguments using: $ java HelloJava.java World!. This differs from executing a pre-compiled Java program, where you would first compile the source into bytecode using javac and then execute the bytecode with java. Here, the command invokes the compilation in-memory and directly runs the program, allowing arguments to be passed just like with a compiled class .
To execute a Java file as a shebang script, you must add a shebang line to the first line of the file: #!/path/to/your/bin/java --source <version>. The file must not have a .java extension and should be made executable with chmod +x. Launching it is then done just like a script, e.g., ./HelloJava. This method provides a script-like execution experience but is limited by its reliance on Unix-like systems, inability to utilize classpath-based JARs without additional configuration, and requiring the Java executable path to be specified correctly .
The main benefit of using the Java shebang method is its simplification of running Java programs in script-like fashion, fostering rapid prototyping and easing the setup of lightweight Java applications in environments supporting scripts. However, its drawbacks include its dependency on Unix-like systems, requirement for executable permissions, and limitations in interacting with complex Java project structures that involve external dependencies. These constraints make it less versatile than regular Java execution methods for larger projects .
Java's single-file source-code program execution primarily limits usage to single-file applications, making it unsuitable for projects requiring complex configurations or multiple files. Libraries outside the JDK must be manually added to the classpath, limiting the seamless integration of third-party dependencies. Additionally, considerations regarding the platform-specific nature of shebang execution and the lack of persistent bytecode for debugging or distribution further limit its applicability to certain scenarios .