karatelabs/karate · error · RuntimeException

toAbsolutePath() needs a path argument

Error message

toAbsolutePath() needs a path argument

What it means

karate.toAbsolutePath(path) converts a relative path to an absolute one, resolved against the current feature file's directory. The library throws this when the function is called with no argument, since there is no path to resolve.

Solutions

  1. Pass the relative path as a string: karate.toAbsolutePath('data/test.json')
  2. If you need the feature directory itself, resolve an empty/'.' path: karate.toAbsolutePath('.')
  3. Verify the argument expression is not accidentally omitted during string building.

Example fix

// before
var abs = karate.toAbsolutePath();
// after
var abs = karate.toAbsolutePath('data/test.json');
Defensive patterns

Strategy: validation

Validate before calling

// JS
if (relPath == null) karate.fail('toAbsolutePath needs a path');
var abs = karate.toAbsolutePath(relPath);

Type guard

function isNonEmptyString(s) { return typeof s === 'string' && s.length > 0; }

Try / catch

try { return karate.toAbsolutePath(p); } catch (e) { karate.warn('toAbsolutePath failed: ' + e); return p; }

Prevention

When it happens

Trigger: `karate.toAbsolutePath()` with zero arguments in a JS block; calling it before supplying the relative path argument.

Common situations: Building the path dynamically and the variable concatenation produced an empty call; misunderstanding the API as taking no args to get the feature directory (use karate.getConstants() or similar instead).

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/cede1fdb75c3560a. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJs.java:1126

                throw new RuntimeException("render() read arg should not be null");
            } else {
                readPath = args[0] + "";
            }
            if (args.length > 1 && args[1] instanceof Map) {
                vars = (Map<String, Object>) args[1];
            }
            return markup().processPath(readPath, vars);
        };
    }

    /**
     * karate.toAbsolutePath(path) - Convert a relative path to absolute.
     * Resolves relative to the current feature file's directory.
     */
    private JavaInvokable toAbsolutePath() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("toAbsolutePath() needs a path argument");
            }
            String path = args[0] + "";
            Resource resource = getCurrentResource().resolve(path);
            if (resource.isFile() && resource.getPath() != null) {
                return resource.getPath().toAbsolutePath().toString();
            }
            // For classpath resources, return the prefixed path
            return resource.getPrefixedPath();
        };
    }

    /**
     * karate.write(value, path) - Write content to a file.
     * Path is relative to the output directory (e.g., target/karate-reports).
     */
    private JavaInvokable write() {
        return args -> {
            if (args.length < 2) {

View on GitHub (pinned to a22eb90246)