Return to site

☕🖥️ SCRIPTING in JAVA 25: 🔟 SH/BAT jobs done in Java

Your next sysadmin script can be a single .java file.

· java

🔸 TLDR

Java 25 makes Java surprisingly practical for scripting.

▪️ No explicit class required

▪️ void main() is enough

▪️ Run directly with java Script.java

▪️ Compact source files automatically import the public APIs exported by java.base

Here are 🔟 everyday SH/BAT-style scripts written as minimal Java 25 files.

Section image

📃 1️⃣ LIST A DIRECTORY

Java 25 snippet: List.java

void main(String[] args) throws Exception {
    try (var files = Files.list(Path.of(args.length == 0 ? "." : args[0]))) {
        files.forEach(System.out::println);
    }
}

Equivalent to a basic ls / dir.

Run:

java List.java .

🔎 2️⃣ FIND FILES RECURSIVELY

Java 25 snippet: Find.java

void main(String[] args) throws Exception {
    try (var files = Files.walk(Path.of(args[0]))) {
        files.filter(p -> p.toString().endsWith(args[1]))
             .forEach(System.out::println);
    }
}

Find matching files through a directory tree.

Run:

java Find.java logs .log

👓 3️⃣ GREP A TEXT FILE

Java 25 snippet: Grep.java

void main(String[] args) throws Exception {
    try (var lines = Files.lines(Path.of(args[0]))) {
        lines.filter(l -> l.contains(args[1]))
             .forEach(System.out::println);
    }
}

A minimal Java equivalent of grep.

Run:

java Grep.java app.log ERROR

©️ 4️⃣ COPY A FILE

Java 25 snippet: Copy.java

Exception {
    Files.copy(
        Path.of(args[0]),
        Path.of(args[1]),
        StandardCopyOption.REPLACE_EXISTING
    );
}

Copy a file and replace the destination if it already exists.

Run:

java Copy.java app.log backup.log

🚚 5️⃣ MOVE OR RENAME A FILE

Java 25 snippet: Move.java

void main(String[] args) throws Exception {
    Files.move(
        Path.of(args[0]),
        Path.of(args[1]),
        StandardCopyOption.REPLACE_EXISTING
    );
}

The scripting equivalent of mv or move.

Run:

java Move.java old.log archive.log
Section image

🗑️ 6️⃣ DELETE OLD LOGS

Java 25 snippet: CleanLogs.java

void main(String[] args) throws Exception {
    var limit = Instant.now().minus(Duration.ofDays(7));

    try (var files = Files.find(Path.of(args[0]), 99,
        (p, a) -> p.toString().endsWith(".log")
            && a.lastModifiedTime().toInstant().isBefore(limit))) {

        files.forEach(p -> p.toFile().delete());
    }
}

Recursively remove .log files older than seven days.

Run:

java CleanLogs.java logs

📊 7️⃣ CHECK FREE DISK SPACE

Java 25 snippet: Disk.java

void main() throws Exception {
    var disk = Files.getFileStore(Path.of("."));

    System.out.printf(
        "Free: %.2f GB%n",
        disk.getUsableSpace() / 1_000_000_000.0
    );
}

Quick disk-space check without invoking an OS command.

Run:

java Disk.java

🌐 8️⃣ CHECK AN HTTP ENDPOINT

Java 25 snippet: HttpCheck.java

void main(String[] args) throws Exception {
    var c = (HttpURLConnection)
        URI.create(args[0]).toURL().openConnection();

    c.setConnectTimeout(3000);
    System.out.println(c.getResponseCode());
}

Call an HTTP endpoint and print its response status.

Run:

java HttpCheck.java https://spring.io

🔌 9️⃣ CHECK A TCP PORT

Java 25 snippet: PortCheck.java

void main(String[] args) {
    try (var socket = new Socket()) {
        socket.connect(
            new InetSocketAddress(args[0], Integer.parseInt(args[1])),
            2000
        );
        System.out.println("OPEN");
    } catch (IOException e) {
        System.out.println("CLOSED");
    }
}

Test whether a host/port can be reached.

Run:

java PortCheck.java localhost 8080

▶️ 🔟 RUN AN OS COMMAND

Java 25 snippet: Run.java

void main(String[] args) throws Exception {
    var exit = new ProcessBuilder(args)
        .inheritIO()
        .start()
        .waitFor();

    System.exit(exit);
}

Execute another program while forwarding its console and exit code.

Linux:

java Run.java ping -c 3 localhost

Windows:

java Run.java cmd /c dir;

🔸 TAKEAWAYS

▪️ Java 25 can now feel much closer to a scripting language for small utilities.

▪️ java MyScript.java means no explicit javac step is required.

▪️ Compact source files remove the class boilerplate and allow void main().

▪️ You still get Java APIs for files, networking, processes, dates and streams.

▪️ SH/BAT remains perfect for tiny OS-specific commands. Java becomes interesting when the script starts growing into actual logic.

Maybe the boundary between "script" and "Java program" just became much thinner. ☕

#Java #Java25 #JDK25 #OpenJDK #Scripting #Automation #DevOps #SysAdmin

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇