Run a WildFly Application with JBang in Minutes

This tutorial shows how to use JBang scripts to launch a WildFly server, create a simple Jakarta REST endpoint, and extend the server with WildFly Glow add‑ons such as Kafka, providing step‑by‑step commands, code examples, and Docker integration for rapid prototyping.

JakartaEE China Community
JakartaEE China Community
JakartaEE China Community
Run a WildFly Application with JBang in Minutes

Why run WildFly from a Java script?

WildFly provides a standard API set for Java applications. Integrating it with JBang lets you prototype enterprise‑grade microservices quickly without creating a full project or managing server configuration, because all setup is expressed via JBang annotations.

Prerequisites

Install JBang ("JBang: Create Java scripts like a pro").

(Optional) Install WildFly Glow for advanced script control.

Basic example

The WildFly Glow documentation offers a minimal script that defines a Jakarta REST service:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
//DEPS org.wildfly.bom:wildfly-expansion:${wildfly.version:35.0.1.Final}@pom
//DEPS org.wildfly.glow:wildfly-glow:1.4.1.Final
//DEPS jakarta.ws.rs:jakarta.ws.rs-api
//DEPS jakarta.enterprise:jakarta.enterprise.cdi-api

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/")
public class myapp extends Application {
    @Path("/hello")
    @ApplicationScoped
    public static class Hello {
        @GET
        public String sayHello() {
            return "Hello, WildFly!";
        }
    }
}

Run the script with jbang myapp.java. JBang resolves the dependencies, starts WildFly, and you can test the endpoint with curl http://localhost:8080/myapp/hello, which returns Hello, WildFly!.

Extending the default WildFly installation

WildFly Glow can list available add‑ons: ./wildfly-glow show-add-ons The output includes add‑ons for clustering, databases, Kafka, etc. To include Kafka, add the annotation //GLOW --add-ons=kafka.

Revisiting the basic example with Kafka

The script below adds Kafka support and uses MicroProfile Config to inject a configurable name:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
//DEPS org.wildfly.bom:wildfly-expansion:${wildfly.version:35.0.1.Final}@pom
//DEPS org.wildfly.glow:wildfly-glow:1.4.1.Final
//DEPS jakarta.ws.rs:jakarta.ws.rs-api
//DEPS jakarta.enterprise:jakarta.enterprise.cdi-api
//DEPS org.eclipse.microprofile.config:microprofile-config-api
//DEPS org.apache.kafka:kafka-clients:3.6.0
//GLOW --add-ons=kafka

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Application;
import java.util.Properties;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

@ApplicationPath("/")
public class myapp extends Application {
    @Path("/hello")
    @ApplicationScoped
    public static class Hello {
        @Inject
        @ConfigProperty(name = "nick.name", defaultValue = "WildFly")
        private String configName;

        @GET
        @Path("/{name}")
        public String sayHelloWithParam(@PathParam("name") String name) {
            return sendMessage(name);
        }

        @GET
        public String sayHelloDefault() {
            return sendMessage(configName);
        }

        private String sendMessage(String nameToUse) {
            String message = String.format("Hello, %s!", nameToUse);
            Properties props = new Properties();
            props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
            props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
            props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
            try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
                producer.send(new ProducerRecord<>("greetings", "key", message));
            } catch (Exception e) {
                return "Failed to send message to Kafka: " + e.getMessage();
            }
            return message;
        }
    }
}

Before running the app, start a Kafka broker, e.g. with Docker: docker run -p 9092:9092 apache/kafka:4.0.0 Run the script again ( jbang myapp.java) and test the endpoints: curl http://localhost:8080/myapp/hello returns the default greeting; curl http://localhost:8080/myapp/hello/Frank returns a personalized greeting and sends the message to the Kafka topic greetings. To view messages, use the Kafka console consumer:

/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic greetings --from-beginning

Conclusion

The combination of WildFly, JBang, and WildFly Glow lets you embed a full Java EE application server in a single script, enabling rapid development, testing, and prototyping of enterprise features such as Kafka integration without a traditional project structure.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Kafkajakarta-eewildflyJava scriptingJBangWildFly Glow
JakartaEE China Community
Written by

JakartaEE China Community

JakartaEE China Community, official website: jakarta.ee/zh/community/china; gitee.com/jakarta-ee-china; space.bilibili.com/518946941; reply "Join group" to get QR code

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.