Bootstrapping Weld CDI from command line with Java SE

2 Minutes reading time

It is possible to use CDI and Weld in a Java command line program. There are several options to bootstrap the CDI container. Weld offers a special Main class that does the job for us. But sometimes we just want to shield the CDI dependencies and provide our own main method. Now comes the tricky part: how do we pass command line arguments to the container, or even boot managed beans by injecting command line argument?

The answer is simple: use a static producer method! Here is an example:

public final class Main {
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    private static Configuration CONFIGURATION;
    public static Configuration configuration() {
        return CONFIGURATION;

    ProjectSpecificBean instance;
    public void run() throws IOException {
        // Use injected instace
    public static void main(String[] args) throws IOException {
        // Initialize the Configurtation instance by command line arguments
        // and store it in a static field. Now it is available by the producer method
        CONFIGURATION = new Configuration(args);

        // Initialize Weld
        Weld theWeld = new Weld();
        WeldContainer theContainer = theWeld.initialize();

        // Execute the run method

        // Shutting down Weld again

Note that ProjectSpecificBean requires a container managed Configuration instance. This is acquired by the configuration() static producer method.

Quite simple. I really love CDI, even in a Java SE environment.

Git revision: 63a36b0

Loading comments...