The spring boot error java.lang.IllegalArgumentException: Sources must not be empty occurs if the source class name is not specified in the SpringApplicationBuilder. When the spring boot application starts, this error is thrown in the console. SpringApplicationBuilder creates and loads the spring boot application using the name of the source class and executes it using the arguments.

There are two types of IllegalArgumentException that are thrown while starting the spring boot application.

java.lang.IllegalArgumentException: Sources must not be empty
java.lang.IllegalArgumentException: Args must not be empty

The run api in the SpringApplication is used to start a spring boot application. The run api has two arguments, source class name and the arguments. If any of the argument is null then java.lang.IllegalArgumentException will be thrown.

The main method accepts command line arguments as string arrays. The string array may be either an empty array or a number of arguments. If the array is set to null, then this error is thrown from the application



Exception

2020-07-17 15:45:37.745 ERROR 13301 --- [           main] o.s.boot.SpringApplication               : Application run failed
java.lang.IllegalArgumentException: Sources must not be empty
	at org.springframework.util.Assert.notEmpty(Assert.java:464) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:385) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at com.yawintutor.SpringHelloWorldApplication.main(SpringHelloWorldApplication.java:14) [classes/:na]
ERROR org.springframework.boot.SpringApplication - Application run failed
 java.lang.IllegalArgumentException: Args must not be null
     at org.springframework.util.Assert.notNull(Assert.java:198)
     at org.springframework.boot.DefaultApplicationArguments.(DefaultApplicationArguments.java:41)
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
     at com.yawintutor.SpringHelloWorldApplication.main(SpringHelloWorldApplication.java:11)


Error Code

In the below example, value of the argument is set as null

package com.yawintutor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringHelloWorldApplication {
    public static void main(String[] args) {
        args = null;
        SpringApplication.run(SpringHelloWorldApplication.class, args);
    }
}


Root Cause

Spring boot application can not accept null as argument. Spring boot application accepts a string array as argument.



Solution 1 – Sources must not be empty

There are two ways to create “java.lang.IllegalArgumentException: Sources must not be empty” exception in the main method. The first method is using SpringApplication class and the second method is using SpringApplicationBuilder.

package com.yawintutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringHelloWorldApplication {
	public static void main(String[] args) {
		SpringApplication.run((Class)null, args);
	}
}

Output

2020-07-17 16:13:32.876 ERROR 16297 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalArgumentException: Source must not be null
	at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]

Solution

package com.yawintutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringHelloWorldApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringHelloWorldApplication.class, args);
	}
}


Solution 2 – Sources must not be empty

The below example uses the SpringApplicationBuilder. The source class is not passed to create this exception. The solution contains the actual working code.

package com.yawintutor;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringHelloWorldApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder()
		.registerShutdownHook(true)
		.run(args);
	}
}

Output

2020-07-17 16:16:46.874 ERROR 16763 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalArgumentException: Sources must not be empty
	at org.springframework.util.Assert.notEmpty(Assert.java:464) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]

Solution

package com.yawintutor;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringHelloWorldApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder(SpringHelloWorldApplication.class)
		.registerShutdownHook(true)
		.run(args);
	}
}


Solution 3 – Args must not be empty

The spring boot arguments may be either an empty array or a number of arguments. If the argument is null, then set as an empty string array

package com.yawintutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringHelloWorldApplication {

	public static void main(String[] args) {
		args = null;
		SpringApplication.run(SpringHelloWorldApplication.class, args);
	}

}

Output

16:20:13.641 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalArgumentException: Args must not be null
	at org.springframework.util.Assert.notNull(Assert.java:198)

Solution

package com.yawintutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringHelloWorldApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringHelloWorldApplication.class, args);
	}
}


Solution 4 – Args must not be empty

The below example uses the SpringApplicationBuilder. The argument is not passed to create this exception. The solution section contains the actual working code.

package com.yawintutor;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringHelloWorldApplication {
	public static void main(String[] args) {
		args=null;
		new SpringApplicationBuilder(SpringHelloWorldApplication.class)
		.registerShutdownHook(true)
		.run(args);
	}
}

Output

16:23:24.062 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalArgumentException: Args must not be null
	at org.springframework.util.Assert.notNull(Assert.java:198)
	at org.springframework.boot.DefaultApplicationArguments.<init>(DefaultApplicationArguments.java:41)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
	at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)
	at com.yawintutor.SpringHelloWorldApplication.main(SpringHelloWorldApplication.java:14)

Solution

package com.yawintutor;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringHelloWorldApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder(SpringHelloWorldApplication.class)
		.registerShutdownHook(true)
		.run(args);
	}
}

How to create simple Spring Boot Application with Main method



Leave a Reply