BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Simplifying Java Development: Introducing Multi-File Program Launching

Simplifying Java Development: Introducing Multi-File Program Launching

JEP 458, Launch Multi-File Source-Code Programs, has been Closed/Delivered for JDK 22. This JEP proposes to enhance the Java Launcher to execute an application supplied as one or more files of Java source code. This allows a more gradual transition from small applications to larger ones by postponing a full-blown project setup.

Previously, with JEP 330 introduced in JDK 11, developers could execute single-file source-code programs directly using the java command, but this facility was limited to programs contained within a single .java file. The new enhancement extends this capability to multi-file source-code programs, fostering a more seamless development process by reducing the initial setup requirements and allowing for a more exploratory approach to programming.

The motivation behind this update is rooted in the recognition that all large programs begin as small projects. During the early phases of development, the focus is on tinkering and exploration rather than on the final deliverable artifacts. The structure of the project is often fluid, undergoing frequent changes as the development progresses. By enabling the execution of multi-file source-code programs without the need for explicit compilation, the JEP aims to support fast iteration and radical changes, which are crucial during the initial stages of development. This approach significantly lowers the barrier for both experienced and novice developers by eliminating the immediate need to learn and configure additional tools like the Java compiler (javac) or third-party build systems.

The operation of this feature is straightforward yet powerful. For example, suppose a directory contains two files, Prog.java and Helper.java, where each file declares a single class:

// Prog.java
class Prog {
    public static void main(String[] args) { Helper.run(); }
}

// Helper.java
class Helper {
    static void run() { System.out.println("Hello!"); }
}


Running java Prog.java will compile the Prog class in memory and invoke its main method. If the Prog class references the Helper class, the launcher will automatically compile Helper.java in memory as well. This process is dynamically managed by the Java launcher, which compiles and loads classes as needed without enforcing a specific order, thereby facilitating a more flexible development workflow.

To ensure a smooth integration with existing Java projects, the enhancement stipulates that only .java files directly referenced by the program are compiled. This prevents unintended compilation of outdated or experimental code that may reside in the same directory. Moreover, the launcher adheres to Java's packaging conventions, requiring that source files be organized according to their package structure. This structured approach extends to handling modular source-code programs, with the launcher capable of recognizing and compiling modules based on the presence of a module-info.java file in the source tree's root directory.

However, the proposal outlines clear non-goals to manage expectations and scope. It explicitly states that launching multi-file source-code programs via the "shebang" mechanism is not supported, maintaining this feature exclusively for single-file programs. Additionally, it does not aim to simplify the integration of external library dependencies within source-code programs, leaving this consideration for potential future enhancements.

This update to the java application launcher represents a significant step forward in Java's development ecosystem, catering to the evolving needs of the Java community.

About the Author

Rate this Article

Adoption
Style

BT