Launch Multi-File Source-Code Programs

Quick Summary:

  • Lets us run multi-source-file programs directly via the java command, eliminating the need for explicit compilation, ideal for quick testing or scripting in Java.
  • Released in Java 22 (2024).

Example

Before Java 25:

Traditionally, running multiple Java source code files involved multiple steps:

  1. Compile the code using command javac FooBar.java, producing a .class file containing the bytecode.
  2. Run the compiled class file using java FooBar.

After Java 25:

Now, you can directly run multi-file programs from the command line:

  1. Run the program directly from the command line (no compilation required)
  • You now have the ability to directly run multi-file Java programs by specifying only the main class file when executing from the command line.
  • This feature automatically resolves and compiles other related source files located in the same directory or subdirectories based on the dependencies found within the main file being executed.
  • This is a significant convenience because it eliminates the need to manually compile or list all related files in the execution command, thus simplifying the development and testing process for Java applications.

👩‍💻 Hands-on Demo: Multi-File Source-Code Programs

  1. Write a TextUtil-class with a isPalindrome-method that returns true if the String-argument passed to it is indeed a palindrome.
  2. In the main-method of a PalindromeCheckerApp-class, check if there are any args provided during launch:
    • If so, process each of them, responding with “{text here} is a palindrome” or “{text here} is not a palindrome”, using TextUtil‘s isPalindrome-method.
    • If not, then ask the user for input and process that instead.
  3. Run the program using a single command (both with and without arguments).

Solutions

🕵️‍♂️ Click here to reveal the solutions
public class PalindromeCheckerApp {
    public static void main(String[] args) {
        if(args.length == 0) {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter words you wanna check (separated by space): ");
            args = keyboard.nextLine().split(" ");
        }
        Arrays.stream(args)
                .forEach(a -> System.out.printf("\t- '%s' is %s a palindrome %n", a, TextUtil.isPalindrome(a) ? "" : "not "));
    }
}
public class TextUtil {
    public static boolean isPalindrome(String text) {
        text = text.trim();
        return text.equalsIgnoreCase(new StringBuilder(text).reverse().toString());
    }
}

Summary

  • Streamlined Development Process: Java 25 simplifies Java programming by enabling direct execution of multi-source-file programs from the command line, bypassing the traditional compile step and enhancing developer productivity.
  • Efficient Dependency Management: This feature automatically compiles and resolves dependencies from the same or subdirectories when running the main file, eliminating manual compilation and simplifying code testing and iteration.