Java 25 sane main

New Main call in Java 25

This is already a preview in Java 24. So you can run main(){}. And IO.println(). Sweet codesugar. Java is wrpping your class in the oldfashioned mainargs constr under the hood but nice anyway. Good news for creating tools in Java!

preview.java

void main() {
  IO.println("hi! preview!");
}

//or in joiner.java

//DEPS com.google.guava:guava:32.1.2-jre

class joiner {            // removed ‘public’ and matched file name
    public static void main(String[] args) {
        System.out.println(
            com.google.common.base.Joiner.on(", ")
                .join("JBang", "makes", "Java", "fun"));
    }
}


Java 24

run from cli:

java --enable-preview preview.java 

Java 25:

With out the enable preview flag it runs in Java 25.

java preview.java 

nice example in coffeecoding from Josh And James from minute 59.

//usr/bin/env jbang "$0" "$@" ; exit $?
//SOURCES script@scratches
//PREVIEW
//DEPS org.springframework.boot:spring-boot-starter-web
//JAVA 25

import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
static class HelloController {
    @GetMapping("/hi")
    Map<String, String> hello() {
        return Map.of("message", "hi");
    }
}

@Bean
ApplicationRunner standalone() {
    return __ -> System.out.println("standalone!");
}

void main(String[] args) {
    SpringScript.run(args);
}
Tags: