{"record":{"id":"f98217fde1e93a41","repo":"karatelabs/karate","slug":"exec-argument-must-be-string-array-or-object","errorCode":null,"errorMessage":"exec() argument must be string, array, or object","messagePattern":"exec\\(\\) argument must be string, array, or object","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":830,"sourceCode":"     * karate.exec(['ls', '-la'])\n     * karate.exec({ line: 'ls -la', workingDir: '/tmp' })\n     */\n    @SuppressWarnings(\"unchecked\")\n    private JavaInvokable exec() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"exec() needs at least one argument\");\n            }\n            ProcessBuilder builder = ProcessBuilder.create();\n            Object arg = args[0];\n            if (arg instanceof String) {\n                builder.line((String) arg);\n            } else if (arg instanceof List) {\n                builder.args((List<String>) arg);\n            } else if (arg instanceof Map) {\n                builder = ProcessBuilder.fromMap((Map<String, Object>) arg);\n            } else {\n                throw new RuntimeException(\"exec() argument must be string, array, or object\");\n            }\n            ProcessHandle handle = ProcessHandle.start(builder.build());\n            handle.waitSync();\n            return handle.getStdOut();\n        };\n    }\n\n    /**\n     * karate.fork() - Asynchronous process execution.\n     * Returns ProcessHandle for async control.\n     * Usage:\n     * var proc = karate.fork('ping google.com')\n     * var proc = karate.fork({ args: ['node', 'server.js'], listener: fn })\n     * var proc = karate.fork({ args: [...], start: false })  // deferred start\n     * proc.onStdOut(fn).start()\n     * proc.waitSync()\n     * proc.stdOut\n     * proc.exitCode","sourceCodeStart":812,"sourceCodeEnd":848,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L812-L848","documentation":"Karate's JS `karate.exec()` accepts a command as a string (shell command line), a List/array of args, or a Map (process config). The library throws this RuntimeException when the first argument is none of those types, refusing to guess how to launch a process from an unrecognized value.","triggerScenarios":"Calling karate.exec() with a first argument that is not a String, List, or Map — e.g. a Number (karate.exec(123)), null, a JS object that failed to convert to a Map, or a Boolean.","commonSituations":"Passing a numeric exit code or variable that was expected to be a string; forgetting to stringify a template result; copy-pasting a JS object literal where the library expects an options Map but a primitive slipped through; typos that leave the variable undefined in some scopes (undefined may arrive as a non-matching type).","solutions":["Pass the command as a string: karate.exec('echo hello')","Pass an array of arguments: karate.exec(['ls', '-la'])","Pass a config Map (as accepted by ProcessBuilder.fromMap): karate.exec({ args: ['git', 'status'] })","Log/inspect the argument with karate.log(typeof value) before calling exec() to confirm it is string/array/object"],"exampleFix":"// before\nkarate.exec(8080) // number, unsupported\n// after\nkarate.exec('kill -9 ' + pid) // string command","handlingStrategy":"type-guard","validationCode":"// JS\nfunction isExecArg(v) { return typeof v === 'string' || Array.isArray(v) || (v !== null && typeof v === 'object' && !Array.isArray(v)); }\nif (!isExecArg(arg)) throw new Error('exec() needs string, array, or object, got: ' + typeof arg);","typeGuard":"function isExecArg(v) { return typeof v === 'string' || Array.isArray(v) || (v !== null && typeof v === 'object'); }","tryCatchPattern":"try { karate.exec(arg); } catch (e) { if (('' + e).indexOf('exec() argument must be') !== -1) { karate.log('bad exec arg type: ' + typeof arg); } throw e; }","preventionTips":["Always pass a string, array, or options map to exec()","Stringify dynamic values before passing","Log argument type during test authoring"],"tags":["javascript","process-execution","argument-type"],"backgroundTag":"invalid-argument-value","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"}