{"record":{"id":"bdf2ca5d70c166c0","repo":"karatelabs/karate","slug":"failed-to-open-stream-for","errorCode":null,"errorMessage":"Failed to open stream for: ","messagePattern":"Failed to open stream for: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":1087,"sourceCode":"        }\n        return prevResponse;\n    }\n\n    /**\n     * karate.readAsStream(path) - Read file as InputStream.\n     * Useful for streaming large files without loading into memory.\n     */\n    private JavaInvokable readAsStream() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"readAsStream() needs at least one argument\");\n            }\n            String path = args[0] + \"\";\n            Resource resource = getCurrentResource().resolve(path);\n            try {\n                return resource.getStream();\n            } catch (Exception e) {\n                throw new RuntimeException(\"Failed to open stream for: \" + path, e);\n            }\n        };\n    }\n\n    /**\n     * karate.render(template) - Render HTML template (similar to doc).\n     * Returns the rendered HTML string without sending to doc consumer.\n     */\n    @SuppressWarnings(\"unchecked\")\n    private JavaInvokable render() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"render() needs at least one argument\");\n            }\n            String readPath;\n            Map<String, Object> vars = null;\n            if (args[0] instanceof Map) {\n                Map<String, Object> map = (Map<String, Object>) args[0];","sourceCodeStart":1069,"sourceCodeEnd":1105,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L1069-L1105","documentation":"After resolving the path against the current resource context, karate.readAsStream() calls resource.getStream() to open the file. If opening the stream fails for any reason (missing file, unreadable path, I/O error), Karate wraps the original exception in a RuntimeException carrying \"Failed to open stream for: <path>\" with the underlying cause attached.","triggerScenarios":"Calling karate.readAsStream(path) where the resolved Resource cannot be opened: the file does not exist relative to the feature/classpath root, the path resolves to a directory, file permissions deny reading, or the stream is already consumed/closed.","commonSituations":"Typo in the file name or wrong relative path (file lives in a different module or resource dir); file not copied into test resources; running from a jar where the resource path differs; reading a directory instead of a file; OS-level permission restrictions in CI.","solutions":["Check the cause chained in the exception (getCause()) to see whether it is NoSuchFileException, AccessDeniedException, etc.","Verify the file exists at the resolved location: use karate.read(path) first (its 'file not found' message is clearer) or list the resource directory","Fix the path relative to the current feature — use 'classpath:' prefix for test resources: karate.readAsStream('classpath:data/large.csv')","Ensure the file is included in the build (test resources copy / jar packaging) and is a regular file, not a directory","Fix filesystem permissions on the file in CI environments"],"exampleFix":"// before\nvar stream = karate.readAsStream('data/large.csv'); // fails: file not on resolved path\n\n// after\nvar stream = karate.readAsStream('classpath:data/large.csv'); // correct location verified to exist","handlingStrategy":"try-catch","validationCode":"// Resolve and existence-check the path the same way Karate does before streaming:\n// use karate.read(path) as a cheap probe, or verify via build config that the resource exists:\n// ls src/test/resources/data/large.csv   (must match the relative path passed)","typeGuard":null,"tryCatchPattern":"try {\n  var stream = karate.readAsStream(path);\n} catch (e) {\n  if (('' + e).indexOf('Failed to open stream for:') !== -1) {\n    karate.log('cannot open: ' + path + ' cause: ' + e.javaException.getCause());\n    fail('resource missing or unreadable: ' + path);\n  }\n  throw e;\n}","preventionTips":["Inspect e.javaException.getCause() to distinguish file-not-found from permission and I/O problems","Keep resource files under src/test/resources and reference them with the 'classpath:' prefix","Confirm packaging config includes the resource (surefire/jar resource includes)","In CI, verify file permissions and that the file is a regular file, not a directory"],"tags":["file-io","resource-not-found","stream","karate"],"backgroundTag":"file-open-failed","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}