openjdk/jdk · error · IllegalArgumentException

sourcepath not set

Error message

sourcepath not set

What it means

IllegalArgumentException from GenStubs.run(String sourcepath, File outdir, List<String> classes) — the entry-point contract requires a non-null sourcepath because the tool must resolve each class name to a source file via StandardLocation.SOURCE_PATH. It is a precondition check, not an I/O failure.

Source

Thrown at make/langtools/tools/genstubs/GenStubs.java:125

                outdir = new File(iter.next());
            else if (arg.equals("-sourcepath") && iter.hasNext())
                sourcepath = iter.next();
            else if (arg.startsWith("-"))
                throw new IllegalArgumentException(arg);
            else {
                classes.add(arg);
                while (iter.hasNext())
                    classes.add(iter.next());
            }
        }

        return run(sourcepath, outdir, classes);
    }

    public boolean run(String sourcepath, File outdir, List<String> classes) {
        //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
        if (sourcepath == null)
            throw new IllegalArgumentException("sourcepath not set");
        if (outdir == null)
            throw new IllegalArgumentException("source output dir not set");

        JavacTool tool = JavacTool.create();
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

        try {
            fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
            fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
            List<JavaFileObject> files = new ArrayList<JavaFileObject>();
            for (String c: classes) {
                JavaFileObject fo = fm.getJavaFileForInput(
                        StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
                if (fo == null)
                    error("class not found: " + c);
                else
                    files.add(fo);
            }

View on GitHub (pinned to 88dfb74bbe)

Solutions

  1. Always pass -sourcepath (CLI) or set the srcdir attribute (Ant) before generating stubs.
  2. If calling run() from code, validate arguments before the call (see defense below).

Example fix

// before
genStubs.run(null, outDir, classNames);

// after
genStubs.run(Paths.get("src/java.base/share/classes").toString(), outDir, classNames);
Defensive patterns

Strategy: validation

Validate before calling

// guard the precondition before calling run()
Objects.requireNonNull(sourcepath, "sourcepath not set");
if (!Files.isDirectory(Paths.get(sourcepath)))
    throw new IllegalArgumentException("sourcepath does not exist: " + sourcepath);

Try / catch

try {
    genStubs.run(sourcepath, outdir, classes);
} catch (IllegalArgumentException e) {
    // precondition violation: fix the caller, do not retry
    throw new IllegalStateException("GenStubs misconfigured: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: Calling run() (directly or through the Ant task with fork=false) after argument parsing produced a null sourcepath — e.g. invoking GenStubs main() without -sourcepath, or passing null from a custom driver.

Common situations: Custom build scripts invoking GenStubs programmatically; an Ant task configuration where the srcdir attribute was omitted so its getPath() path never reached run().

Related errors


AI-assisted analysis of openjdk/jdk@88dfb74bbe (2026-08-14). Data as JSON: /api/errors/89a457718530f67d. Report an issue: GitHub.