karatelabs/karate · error · RuntimeException
Failed to open stream for
Error message
Failed to open stream for: {path} What it means
PathResource.getStream() opens the underlying file with Files.newInputStream() and wraps any failure (file deleted between existence check and open, permission denied, is a directory, I/O error) in this RuntimeException with the resource path in the message. Callers receive a stream-backed view of the resource, so a failing open breaks any downstream read.
Solutions
- Print the resolved path in the message and confirm it exists and is a regular file (Files.isRegularFile)
- Check read permissions on the file and its parent directories for the process user
- Re-create the resource from a fresh, verified path rather than a cached one
- If the file is generated by an earlier step, add synchronization/wait so it exists before opening
Example fix
// before
Resource r = Resource.path("missing-report.html");
InputStream is = r.getStream(); // throws if deleted
// after
Resource r = Resource.path("report.html");
if (Files.isRegularFile(Paths.get("report.html"))) {
InputStream is = r.getStream();
} Defensive patterns
Strategy: validation
Validate before calling
// Java: verify the file is openable before getting the stream
Path p = Paths.get("report.html");
if (!Files.isRegularFile(p) || !Files.isReadable(p)) {
throw new IllegalStateException("not a readable file: " + p);
} Try / catch
try {
InputStream is = pathResource.getStream();
} catch (RuntimeException e) {
if (e.getMessage().startsWith("Failed to open stream for:")) {
logger.error("open failed for {} cause: {}", e.getMessage(), e.getCause());
}
} Prevention
- Create PathResource from freshly verified paths; avoid caching across steps that clean temp dirs
- Check read permissions and that the path is not a directory
- Use classpath resources to avoid working-directory drift between environments
When it happens
Trigger: The file was removed or renamed after the PathResource was created; process lacks read permission on the file or a parent directory; the path points to a directory; the file is on an unmounted/network volume that became unavailable; file is locked by another process with incompatible sharing (Windows).
Common situations: Temp files cleaned up mid-run by another process; reading files created by a previous test step that ran in a different working directory; permission differences between local dev and CI user; antivirus/file-lock contention on Windows.
Understand the failure class
Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.
Related errors
- Failed to materialize resource to
- Failed to read text from
- Failed to read bytes from
- ext ' ': resource vanished after validation
- ext ' ': failed reading
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/1a71691411e554c1.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/common/PathResource.java:209
boolean hasExtension = pathStr.matches(".*\\.[a-zA-Z0-9]+$");
base = hasExtension ? path.getParent() : path;
}
Path resolved = base.resolve(childPath);
return new PathResource(resolved, root, classpath, classpathRoot);
}
@Override
public Resource getParent() {
Path parentPath = path.getParent();
return parentPath != null ? new PathResource(parentPath, root, classpath, classpathRoot) : null;
}
@Override
public InputStream getStream() {
try {
return Files.newInputStream(path);
} catch (Exception e) {
throw new RuntimeException("Failed to open stream for: " + path, e);
}
}
@Override
public String getRelativePath() {
return relativePath;
}
@Override
public String getText() {
if (text == null) {
try {
bytes = Files.readAllBytes(path);
text = FileUtils.toString(bytes);
} catch (Exception e) {
throw new RuntimeException("Failed to read text from: " + path, e);
}
}View on GitHub (pinned to a22eb90246)