alibaba/spring-ai-alibaba · error · RuntimeException

Failed to delete JAR files from working directory

Error message

Failed to delete JAR files from working directory

What it means

FileUtils.deleteResourceJarFromWorkDir wraps any IOException encountered while removing extracted JAR files from the working directory in a RuntimeException with this message. It indicates that cleanup of dynamically copied resource JARs failed, typically due to file locks, permissions, or missing files.

Source

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

	 */
	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);
						}
					});
				}
			}
		}
		catch (IOException e) {
			throw new RuntimeException("Failed to delete JAR files from working directory", e);
		}
	}

}

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Close any URLClassLoader or stream holding the JARs open before calling deleteResourceJarFromWorkDir (especially on Windows).
  2. Verify the process user has write/delete permission on the working directory.
  3. Check the wrapped IOException cause to identify the exact file that failed and delete it manually.
  4. Make cleanup idempotent/retried at shutdown rather than mid-run.

Example fix

// before
classLoader.close();
fileUtils.deleteResourceJarFromWorkDir();
// after
try (URLClassLoader cl = new URLClassLoader(urls)) {
    // use cl
}
cl.close();
fileUtils.deleteResourceJarFromWorkDir();
Defensive patterns

Strategy: try-catch

Validate before calling

// before cleanup
try (var walk = Files.walk(workDir)) {
  boolean allWritable = walk.allMatch(p -> Files.isWritable(p));
  if (!allWritable) throw new IllegalStateException("work dir not writable");
}

Try / catch

try {
  fileUtils.deleteResourceJarFromWorkDir();
} catch (RuntimeException e) {
  Throwable cause = e.getCause(); // IOException
  log.warn("JAR cleanup failed: {}", cause == null ? "" : cause.getMessage());
}

Prevention

When it happens

Trigger: Calling deleteResourceJarFromWorkDir when the work-dir JAR files cannot be walked or deleted: IOExceptions from Files.walk/delete during cleanup (e.g. JAR still locked by a classloader, read-only directory, file already removed).

Common situations: On Windows where the JAR is held open by a URLClassLoader that was not closed; running under a user without write permission to the working directory; a concurrent process deleting the same files.

Related errors


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