{"record":{"id":"3d3ec0921a1a2bf7","repo":"karatelabs/karate","slug":"exec-needs-at-least-one-argument","errorCode":null,"errorMessage":"exec() needs at least one argument","messagePattern":"exec\\(\\) needs at least one argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":819,"sourceCode":"            return null;\n        };\n    }\n\n    // ========== Process Execution ==========\n\n    /**\n     * karate.exec() - Synchronous process execution.\n     * Returns stdout as string.\n     * Usage:\n     * karate.exec('ls -la')\n     * 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","sourceCodeStart":801,"sourceCodeEnd":837,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L801-L837","documentation":"karate.exec() runs an OS process, accepting either a command-line string or an options object ({ line, workingDir, ... }). The library throws this error when it is called with no arguments, because there is no command to execute; the ProcessBuilder would have nothing to run.","triggerScenarios":"Calling karate.exec() with zero arguments — typically when the command string or options object variable is undefined in the JS scope, or an empty options object was expected to carry defaults (it does not; at least one argument is required).","commonSituations":"Driving commands from environment-driven config where the command variable is unset; calling karate.exec() expecting it to inherit a previously configured command; refactors that moved the command into a variable that resolved to undefined.","solutions":["Pass a command string: karate.exec('ls -la').","Or pass an options object with at least a line field: karate.exec({ line: 'ls -la', workingDir: '/tmp' }).","Verify the command variable is defined and non-empty before the call, especially when sourced from config or env.","Note karate.exec is for running processes; for shell steps inside scenarios use the exec/eval keywords in the feature file instead."],"exampleFix":"// before\nkarate.exec()\n\n// after\nkarate.exec({ line: 'mvn test', workingDir: '/tmp/project' })","handlingStrategy":"validation","validationCode":"// JS, before calling\nif (typeof cmd === 'string' && cmd.length > 0) {\n  karate.exec(cmd);\n} else if (cmd && typeof cmd === 'object' && typeof cmd.line === 'string') {\n  karate.exec(cmd);\n} else {\n  throw new Error('exec() requires a command string or an object with a line field');\n}","typeGuard":"function isExecArg(a) {\n  return (typeof a === 'string' && a.length > 0) ||\n         (a !== null && typeof a === 'object' && typeof a.line === 'string');\n}","tryCatchPattern":"try {\n  karate.exec(commandOrOptions);\n} catch (e) {\n  if (String(e.message).indexOf('exec() needs at least one argument') !== -1) {\n    throw new Error('exec() called without a command — check config/env providing it');\n  }\n  throw e;\n}","preventionTips":["Always pass a command string or an options object containing at least { line: ... }.","Do not assume exec() inherits a previous command; each call needs its own argument.","When the command comes from config/env, fail early with a clear message if it is unset.","Validate that options objects are not empty before passing them."],"tags":["process","argument-count","karate"],"backgroundTag":"missing-required-argument","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"}