{"record":{"id":"887c12a38137a994","repo":"alibaba/spring-ai-alibaba","slug":"failed-to-copy-jar-file","errorCode":null,"errorMessage":"Failed to copy JAR file: ","messagePattern":"Failed to copy JAR file: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java","lineNumber":97,"sourceCode":"\t\t\t\tthrow new RuntimeException(\"Could not find lib directory in resources\");\n\t\t\t}\n\n\t\t\t// Create target directory if it doesn't exist\n\t\t\tPath targetDir = Path.of(workDir);\n\t\t\tif (!Files.exists(targetDir)) {\n\t\t\t\tFiles.createDirectories(targetDir);\n\t\t\t}\n\n\t\t\t// Get all JAR files from lib directory\n\t\t\tPath libPath = Path.of(libUrl.toURI());\n\t\t\ttry (var stream = Files.walk(libPath)) {\n\t\t\t\tstream.filter(path -> path.toString().endsWith(\".jar\")).forEach(jarPath -> {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tPath targetPath = targetDir.resolve(jarPath.getFileName());\n\t\t\t\t\t\tFiles.copy(jarPath, targetPath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (IOException e) {\n\t\t\t\t\t\tthrow new RuntimeException(\"Failed to copy JAR file: \" + jarPath, e);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new RuntimeException(\"Failed to copy JAR files to working directory\", e);\n\t\t}\n\t}\n\n\t/**\n\t * Deletes all JAR files from the specified working directory.\n\t * @param workDir The working directory from which the JAR files will be deleted.\n\t */\n\tpublic static void deleteResourceJarFromWorkDir(String workDir) {\n\t\ttry {\n\t\t\tPath workDirPath = Path.of(workDir);\n\t\t\tif (Files.exists(workDirPath)) {\n\t\t\t\ttry (var stream = Files.walk(workDirPath)) {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java#L79-L115","documentation":"During copyResourceJarToWorkDir, each discovered .jar path in the resources lib directory is copied into the target working directory with REPLACE_EXISTING. An IOException while copying any single JAR is wrapped in this RuntimeException carrying the JAR path.","triggerScenarios":"Files.copy fails for a specific jarPath: target directory not writable, disk full, source JAR unreadable (permissions), or the source is a nested-jar URL whose FileSystem was closed mid-walk.","commonSituations":"Read-only work directory (container filesystem restrictions), insufficient disk space, Spring Boot nested jar resource streams closed before copy, or concurrent processes competing for the same target file.","solutions":["Read e.getCause() (IOException) and check the target workDir is writable and has free space.","Ensure the work directory exists and the process user has write permission (chmod / run with proper user).","Copy JARs once at startup and reuse, avoiding repeated replace races with concurrent workers.","For Spring Boot fat jars, copy the resource stream via Files.copy(InputStream, target) instead of Path-to-Path if the source lives inside a nested jar."],"exampleFix":"// before\nFiles.copy(jarPath, targetPath, REPLACE_EXISTING); // IOException if target dir read-only\n// after\nFiles.createDirectories(targetDir);\nif (!Files.isWritable(targetDir)) {\n    throw new IOException(\"Work dir not writable: \" + targetDir);\n}\ntry (InputStream in = jarUrl.openStream()) {\n    Files.copy(in, targetPath, REPLACE_EXISTING);\n}","handlingStrategy":"try-catch","validationCode":"Files.createDirectories(Path.of(workDir));\nif (!Files.isWritable(Path.of(workDir))) {\n    throw new IllegalStateException(\"workDir is not writable: \" + workDir);\n}","typeGuard":null,"tryCatchPattern":"try {\n    FileUtils.copyResourceJarToWorkDir(workDir);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Failed to copy JAR file:\")) {\n        logger.error(\"Per-jar copy failed: {} cause: {}\", e.getMessage(), e.getCause());\n    }\n    throw e;\n}","preventionTips":["Ensure workDir exists and is writable before copying (container users, volumes).","Monitor disk space in CI/container environments.","Perform the copy once at startup, not per request, to avoid races.","For nested jars, copy via streams rather than Path-to-Path."],"tags":["file-copy","io","jar"],"backgroundTag":"file-write-failed","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}