{"record":{"id":"22e5b52afbfae471","repo":"karatelabs/karate","slug":"failed-to-read-bytes-from-path","errorCode":null,"errorMessage":"Failed to read bytes from: ${path}","messagePattern":"Failed to read bytes from: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":502,"sourceCode":"            Object fallback = args.length > 1 ? args[1] : null;\n            Object first = args[0];\n            if (first == null) return fallback;\n            String value = getProperty(first.toString());\n            return (value == null || value.isEmpty()) ? fallback : value;\n        };\n    }\n\n    private JavaInvokable readAsBytes() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"readAsBytes() needs at least one argument\");\n            }\n            String path = args[0] + \"\";\n            Resource resource = getCurrentResource().resolve(path);\n            try (java.io.InputStream is = resource.getStream()) {\n                return is.readAllBytes();\n            } catch (java.io.IOException e) {\n                throw new RuntimeException(\"Failed to read bytes from: \" + path, e);\n            }\n        };\n    }\n\n    private JavaInvokable get() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"get() needs at least one argument\");\n            }\n            String expr = args[0] + \"\";\n\n            Object result;\n            // a $-prefix always means a path expression, never a variable name - isSimpleIdentifier\n            // accepts '$' as a leading char, so without this `karate.get('$x')` would hunt for a\n            // variable literally called \"$x\" instead of resolving the bare `$varname` form\n            if (!expr.startsWith(\"$\") && isSimpleIdentifier(expr)) {\n                result = engine.get(expr);\n            } else {","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L484-L520","documentation":"After resolving the path, readAsBytes() opens the resource stream and reads all bytes; if the underlying IO fails with an IOException, Karate wraps it in this message including the path and the original cause. It means the file was resolved but its bytes could not be read (missing file, unreadable stream, IO error).","triggerScenarios":"karate.readAsBytes('...') where the resource stream throws IOException — typically the file does not exist at the resolved location, the path is wrong, or the file is unreadable/locked.","commonSituations":"Typo'd classpath paths; file present in dev but not packaged into the test classpath jar; permissions issues in CI containers.","solutions":["Verify the file exists at the given path/classpath location (print it or list the directory).","Fix the path prefix (classpath:, file:, relative) so resolution points at the right root.","Ensure the resource is included in the build (test resources packaging) when running from a jar.","Check file read permissions for the process user."],"exampleFix":"// before\nvar bytes = karate.readAsBytes('classpath:files/repot.pdf');\n// after\nvar bytes = karate.readAsBytes('classpath:files/report.pdf');","handlingStrategy":"try-catch","validationCode":"var path = 'classpath:files/report.pdf';\n// verify presence before reading\nvar exists = karate.readAsString('classpath:files/manifest.txt') != null; // or list assets in config","typeGuard":null,"tryCatchPattern":"var bytes;\ntry { bytes = karate.readAsBytes(path); }\ncatch (e) { if ((e.message || '').indexOf('Failed to read bytes from') >= 0) karate.fail('resource missing/unreadable: ' + path); throw e; }","preventionTips":["Double-check path spelling and prefixes before running.","Ensure binary files are packaged into the jar/test classpath in CI.","Check file permissions for the user running tests.","Catch the IOException-wrapped message and log e.cause for diagnosis."],"tags":["io","file-read","karate"],"backgroundTag":"file-read-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"}