apache/beam · error · CompileException

${diagnostics}

Error message

${diagnostics}

What it means

StringCompiler compiles in-memory Java source with the javax.tools compiler. When task.call() returns false (compilation failed), it throws CompileException carrying the collected Diagnostics (syntax errors, missing symbols, bad classpath). Callers like getInstance wrap it in RuntimeException.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/providers/StringCompiler.java:118

  // TODO(XXX): swap args?
  public static <T> Class<T> getClass(String name, String source)
      throws CompileException, ClassNotFoundException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    InMemoryFileManager fileManager =
        new InMemoryFileManager(compiler.getStandardFileManager(null, null, null));
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    JavaCompiler.CompilationTask task =
        compiler.getTask(
            null,
            fileManager,
            diagnostics,
            ImmutableList.of("-classpath", classpathSupplier.get()),
            null,
            Collections.singletonList(new InMemoryFileManager.InputJavaFileObject(name, source)));
    boolean result = task.call();
    if (!result) {
      throw new CompileException(diagnostics);
    } else {
      return (Class<T>) fileManager.getClassLoader().loadClass(name);
    }
  }

  public static Object getInstance(String name, String source)
      throws CompileException, ReflectiveOperationException {
    return getClass(name, source).getDeclaredConstructor().newInstance();
  }

  public static Type guessExpressionType(String expression, Map<String, Type> inputTypes)
      throws StringCompiler.CompileException, ClassNotFoundException {

    String expectedError = "cannot be converted to __TypeGuesserHelper__.BadReturnType";

    try {
      StringCompiler.getClass(
          "__TypeGuesserHelper__", typeGuesserSource(expression, inputTypes, "BadReturnType"));

View on GitHub (pinned to 12126d8942)

Solutions

  1. Inspect the Diagnostics in the CompileException for the concrete compiler errors
  2. Fix the generated/expression source (check field names against the input schema)
  3. Verify the classpath supplier includes all needed dependencies
  4. Ensure the JDK (not JRE-only runtime) is used so javax.tools is available

Example fix

// before
expression: "input.nmae"
// after
expression: "input.name"
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check: expression references exist in input schema
if (!inputSchema.hasField(fieldName)) throw new IllegalArgumentException("Unknown field: " + fieldName);

Try / catch

try { Class<?> c = StringCompiler.getInstance(name, source); }
catch (RuntimeException e) {
  if (e.getCause() instanceof StringCompiler.CompileException) {
    ((StringCompiler.CompileException) e.getCause()).getDiagnostics().forEach(d -> LOG.error(d));
  } else throw e;
}

Prevention

When it happens

Trigger: Compiling generated UDF/filter source via getClass() where the source has syntax errors, references unknown symbols/fields, or the classpathSupplier's classpath is wrong/incomplete so imports don't resolve.

Common situations: Filter expressions generating code referencing nonexistent schema fields; running in environments where the compiler isn't available or classpath is truncated (shaded/fat jars missing javax.tools dependencies); expression syntax mistakes.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/2273cf6bbeb4bda4. Report an issue: GitHub.