spring-projects/spring-boot · error · MojoExecutionException

Unable to build classpath

Error message

Unable to build classpath

What it means

MojoExecutionException thrown by AbstractRunMojo.getClassPathUrls when an IOException occurs while building the URL list for the forked run classpath. Unlike the broader sibling in addClasspath, this catch is scoped to IOException only and originates inside getClassPathUrls itself — emitted by addResources/addProjectClasses/addDependencies or URL array conversion.

Solutions

  1. Run `mvn clean compile` first so target/classes and target/test-classes exist before spring-boot:run.
  2. Inspect the chained IOException for the offending path.
  3. Run `mvn dependency:resolve` to ensure every runtime artifact file is present on disk.
  4. Remove any stale <additionalClasspathElements> pointing at deleted directories.

Example fix

// before: mvn spring-boot:run   (before compile, target/classes absent)
// after:  mvn clean compile spring-boot:run
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-flight: ensure the standard class folders exist before run:
List<String> required = List.of("target/classes", "target/test-classes");
for (String p : required) {
    if (!new File(p).isDirectory()) {
        // run `mvn compile` first
    }
}

Try / catch

try {
    // invoke run goal
} catch (MojoExecutionException ex) {
    if ("Unable to build classpath".equals(ex.getMessage()) && ex.getCause() instanceof IOException) {
        // most likely missing output folder — run `mvn clean compile` and retry
    }
    throw ex;
}

Prevention

When it happens

Trigger: An IOException raised by addResources, addProjectClasses, addDependencies, or the URL construction during spring-boot:run classpath assembly. E.g., a File.toURI().toURL() chain throws, or a filesystem walk over resources fails.

Common situations: Output directories (target/classes, target/test-classes) missing or unreadable; a dependency artifact file path is stale; IDE reimport left the local repo inconsistent; running run goal before compile/test-compile phase so required class folders don't exist yet.

Related errors


AI-assisted analysis of spring-projects/spring-boot@270dfe353f (2026-08-11). Data as JSON: /api/errors/0273abfb2d486738. Report an issue: GitHub.

Appendix: source

Thrown at build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java:407

			}
			args.addAll(classpath.args(true));
		}
		catch (Exception ex) {
			throw new MojoExecutionException("Could not build classpath", ex);
		}
	}

	protected URL[] getClassPathUrls() throws MojoExecutionException {
		try {
			List<URL> urls = new ArrayList<>();
			addAdditionalClasspathLocations(urls);
			addResources(urls);
			addProjectClasses(urls);
			addDependencies(urls);
			return urls.toArray(new URL[0]);
		}
		catch (IOException ex) {
			throw new MojoExecutionException("Unable to build classpath", ex);
		}
	}

	private void addAdditionalClasspathLocations(List<URL> urls) throws MalformedURLException {
		if (this.additionalClasspathElements != null) {
			for (String element : this.additionalClasspathElements) {
				urls.add(new File(element).toURI().toURL());
			}
		}
	}

	private void addResources(List<URL> urls) throws IOException {
		if (this.addResources) {
			for (Resource resource : this.project.getResources()) {
				File directory = new File(resource.getDirectory());
				urls.add(directory.toURI().toURL());
				for (File classesDirectory : getClassesDirectories()) {
					FileUtils.removeDuplicatesFromOutputDirectory(classesDirectory, directory);

View on GitHub (pinned to 270dfe353f)