alibaba/spring-ai-alibaba · error · RuntimeException

Failed to copy JAR files to working directory

Error message

Failed to copy JAR files to working directory

What it means

The outer catch-all of copyResourceJarToWorkDir wraps any exception escaping the copy procedure — including the resource-not-found error, per-file copy failures, and directory creation errors — into this RuntimeException.

Source

Thrown at spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java:103

				Files.createDirectories(targetDir);
			}

			// Get all JAR files from lib directory
			Path libPath = Path.of(libUrl.toURI());
			try (var stream = Files.walk(libPath)) {
				stream.filter(path -> path.toString().endsWith(".jar")).forEach(jarPath -> {
					try {
						Path targetPath = targetDir.resolve(jarPath.getFileName());
						Files.copy(jarPath, targetPath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
					}
					catch (IOException e) {
						throw new RuntimeException("Failed to copy JAR file: " + jarPath, e);
					}
				});
			}
		}
		catch (Exception e) {
			throw new RuntimeException("Failed to copy JAR files to working directory", e);
		}
	}

	/**
	 * Deletes all JAR files from the specified working directory.
	 * @param workDir The working directory from which the JAR files will be deleted.
	 */
	public static void deleteResourceJarFromWorkDir(String workDir) {
		try {
			Path workDirPath = Path.of(workDir);
			if (Files.exists(workDirPath)) {
				try (var stream = Files.walk(workDirPath)) {
					stream.filter(path -> path.toString().endsWith(".jar")).forEach(jarPath -> {
						try {
							Files.deleteIfExists(jarPath);
						}
						catch (IOException e) {
							throw new RuntimeException("Failed to delete JAR file: " + jarPath, e);

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Unwrap the cause chain (getCause()) to find the root failure — often the nested 'Could not find lib directory' or a copy IOException.
  2. Ensure the resources/lib directory is packaged on the classpath.
  3. When loading from Spring Boot jars, open a FileSystem on the jar and keep it open for the duration of Files.walk.
  4. Create the workDir beforehand with correct permissions.

Example fix

// before
catch (Exception e) {
    throw new RuntimeException("Failed to copy JAR files to working directory", e);
}
// after
catch (Exception e) {
    logger.error("copyResourceJarToWorkDir root cause", e);
    throw new RuntimeException("Failed to copy JAR files to working directory: " + e.getMessage(), e);
}
Defensive patterns

Strategy: try-catch

Validate before calling

URL libUrl = FileUtils.class.getClassLoader().getResource("lib");
Files.createDirectories(Path.of(workDir));

Try / catch

try {
    FileUtils.copyResourceJarToWorkDir(workDir);
} catch (RuntimeException e) {
    Throwable root = e;
    while (root.getCause() != null) root = root.getCause();
    logger.error("copyResourceJarToWorkDir failed, root cause: {}", root.toString(), e);
    throw e;
}

Prevention

When it happens

Trigger: Any failure inside copyResourceJarToWorkDir: missing 'lib' resource, unable to create targetDir, Files.walk failing on a closed/detached FileSystem (Spring Boot nested jars), or a wrapped per-JAR IOException bubbling up.

Common situations: Spring Boot nested-jar walking after the zip FileSystem is closed, missing resources/lib packaging, workDir creation denied, or the per-file RuntimeException being double-wrapped and obscuring the real cause.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/c201a13e7fc52193. Report an issue: GitHub.