alibaba/spring-ai-alibaba · error · RuntimeException

Failed to delete JAR file:

Error message

Failed to delete JAR file: 

What it means

FileUtils.deleteResourceJarFromWorkDir walks the working directory and deletes every .jar file via Files.deleteIfExists. An IOException on any individual delete is wrapped in this RuntimeException naming the JAR path.

Source

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

			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);
						}
					});
				}
			}
		}
		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 streams holding the JAR open before deleting.
  2. Verify file/parent directory write permissions for the deleting user.
  3. Retry the delete after the consuming process (e.g. the executed java/shell job) has exited.
  4. Check e.getCause() to distinguish permission denied from file-in-use.

Example fix

// before
Files.deleteIfExists(jarPath); // may fail while jar is locked
// after
if (!classLoaderClosed) {
    classLoader.close(); // release jar handles first
}
boolean deleted = Files.deleteIfExists(jarPath);
if (!deleted) { logger.debug("jar already absent: {}", jarPath); }
Defensive patterns

Strategy: retry

Validate before calling

Path dir = Path.of(workDir);
if (!Files.isDirectory(dir) || !Files.isWritable(dir)) {
    throw new IllegalStateException("workDir missing or not writable: " + workDir);
}

Try / catch

try {
    FileUtils.deleteResourceJarFromWorkDir(workDir);
} catch (RuntimeException e) {
    logger.warn("JAR delete failed ({}), will retry after process exit", e.getMessage());
    // retry later, e.g. scheduled cleanup
}

Prevention

When it happens

Trigger: Files.deleteIfExists fails for a specific jar: file locked by a running process (e.g. a JVM/URLClassLoader still holding the jar open on Windows), permission denied on the file or parent directory, or the path became a directory mid-walk.

Common situations: On Windows, JARs loaded into a classloader remain locked; work directory owned by another user; antivirus briefly locking files; delete attempted while the code-execution process is still running.

Related errors


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