alibaba/spring-ai-alibaba · error · RuntimeException
Could not find lib directory in resources
Error message
Could not find lib directory in resources
What it means
FileUtils.copyResourceJarToWorkDir locates the 'lib' directory on the classpath (expected to contain JAR files bundled in resources) and copies its contents into the working directory. When classLoader.getResource("lib") returns null — the resource is not packaged — this RuntimeException is thrown.
Source
Thrown at spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java:79
Files.deleteIfExists(filepath);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Copies all JAR files from the resources/lib directory to the specified working
* directory.
* @param workDir The target working directory where the JAR files will be copied.
*/
public static void copyResourceJarToWorkDir(String workDir) {
try {
// Get the JAR files from resources/lib directory
ClassLoader classLoader = FileUtils.class.getClassLoader();
URL libUrl = classLoader.getResource("lib");
if (libUrl == null) {
throw new RuntimeException("Could not find lib directory in resources");
}
// Create target directory if it doesn't exist
Path targetDir = Path.of(workDir);
if (!Files.exists(targetDir)) {
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);View on GitHub (pinned to f82da0b50f)
Solutions
- Add the lib directory to src/main/resources and ensure Maven includes it (default resources filtering excludes nothing by default; check <resources> config).
- Rebuild/repackage so lib/*.jar is present inside the artifact.
- If running in Spring Boot, verify the resource is visible via getClass().getClassLoader().getResource("lib") and consider extracting nested-jar resources with FileSystem handling.
- If no bundled JARs are needed, skip the call instead of failing.
Example fix
<!-- before: pom.xml excludes resources --> <resources><resource><directory>src/main/resources</directory><excludes><exclude>lib/**</exclude></excludes></resource></resources> <!-- after: include lib --> <resources><resource><directory>src/main/resources</directory></resource></resources>
Defensive patterns
Strategy: validation
Validate before calling
URL libUrl = FileUtils.class.getClassLoader().getResource("lib");
if (libUrl == null) {
throw new IllegalStateException("lib directory missing from classpath — check packaging");
} Try / catch
try {
FileUtils.copyResourceJarToWorkDir(workDir);
} catch (RuntimeException e) {
if (e.getMessage().contains("Could not find lib directory")) {
logger.error("resources/lib not packaged; verify build includes src/main/resources/lib");
}
throw e;
} Prevention
- Verify the packaged artifact contains lib/ (jar tf app.jar | grep lib).
- Check Maven/Gradle resource includes/excludes after any build config change.
- Test from both IDE and packaged-jar runs — classpath layouts differ.
- Account for Spring Boot nested-jar resource URLs.
When it happens
Trigger: Calling copyResourceJarToWorkDir when the classpath has no 'lib' resource: the lib directory was not included in the JAR, it sits in a module not on the runtime classpath, or it is referenced from a different ClassLoader (e.g. Spring Boot fat-jar nested URLs, custom classloader).
Common situations: Running in a Spring Boot executable jar where resources/lib was excluded by build config, missing maven resource include for the lib folder, running from an IDE with different resource roots, or shading/relocation that dropped the directory.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- not found default-default-application.yml
- Failed to copy JAR files to working directory
- Failed to copy JAR files to working directory
- Could not find executeAgent method in AgentToolExecutor clas
- Cannot instantiate class {} for @class deserialization
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/769fe04707283517.
Report an issue: GitHub.