apple/pkl · error · JavaCodeGeneratorException

Cannot generate Java code for a Pkl standard library module

Error message

Cannot generate Java code for a Pkl standard library module (`${schema.moduleUri}`).

What it means

The javaFile getter refuses to generate Java code when the schema's module URI uses the `pkl:` scheme, i.e. the module is part of the Pkl standard library. Stdlib modules are provided by Pkl itself; generating bindings for them is not meaningful or supported.

Solutions

  1. Target your own module file/URI (e.g. my/module.pkl) instead of a `pkl:` stdlib URI.
  2. If you need stdlib types, depend on the library's pre-built mappings (pkl-config-java) rather than generating them.
  3. Check your PklProject dependencies so imports resolve to your modules, not stdlib.
  4. If you genuinely need stdlib bindings, file an issue upstream.

Example fix

// before
pkl gen java pkl:base
// after
pkl gen java com/example/config/AppConfig.pkl
Defensive patterns

Strategy: validation

Validate before calling

require(!moduleUri.toString().startsWith("pkl:")) { "cannot generate bindings for stdlib module" }

Type guard

fun isStdlibModule(uri: URI) = uri.scheme == "pkl"

Try / catch

try { gen.javaFile } catch (e: JavaCodeGeneratorException) { if ("standard library module" in e.message!!) skipModule() else throw e }

Prevention

When it happens

Trigger: Passing a module URI such as pkl:base or pkl:format directly to the Java code generator, or a project dependency resolving to a stdlib module instead of a user module.

Common situations: Mistyping the target module in the CLI invocation, intending to extend stdlib types, or wiring the codegen to analyze pkl.base rather than your own amir/pkl module.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/094937aa4e0e3894. Report an issue: GitHub.

Appendix: source

Thrown at pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt:196

        }
      return AnnotationSpec.builder(className).build()
    }

  private val javaFileName: String
    get() {
      val (packageName, className) = nameMapper.map(schema.moduleName)
      val dirPath = packageName.replace('.', '/')
      return if (dirPath.isEmpty()) {
        "java/$className.java"
      } else {
        "java/$dirPath/$className.java"
      }
    }

  val javaFile: String
    get() {
      if (schema.moduleUri.scheme == "pkl") {
        throw JavaCodeGeneratorException(
          "Cannot generate Java code for a Pkl standard library module (`${schema.moduleUri}`)."
        )
      }

      val pModuleClass = schema.moduleClass
      val moduleClass = generateTypeSpec(pModuleClass, schema)

      for (pClass in schema.classes.values) {
        moduleClass.addType(generateTypeSpec(pClass, schema).build())
      }

      for (typeAlias in schema.typeAliases.values) {
        val stringLiterals = mutableSetOf<String>()
        if (CodeGeneratorUtils.isRepresentableAsEnum(typeAlias.aliasedType, stringLiterals)) {
          moduleClass.addType(generateEnumTypeSpec(typeAlias, stringLiterals).build())
        }
      }
      // generate static append method for module classes w/o parent class; reuse in subclasses and

View on GitHub (pinned to f3efcbfc9b)