JetBrains/intellij-community · error · IncorrectOperationException

This template did not produce a Java class or an interface\n

Error message

This template did not produce a Java class or an interface\n

What it means

createFromTemplate parses the expanded file-template content by creating a PSI file in Java language; if that PSI file is not a PsiJavaFile at all, an IncorrectOperationException with the full expanded text appended is thrown. It means the template output could not be interpreted as Java source — e.g. the template produced JSON, XML, plain text, or the file template was invoked with the wrong handler for its content.

Source

Thrown at java/java-impl/src/com/intellij/ide/fileTemplates/JavaCreateFromTemplateHandler.java:57

  }

  private static PsiClass createClassOrInterface(Project project,
                                                 PsiDirectory directory,
                                                 String content,
                                                 boolean reformat,
                                                 String extension, @Nullable String optionalClassName) throws IncorrectOperationException {
    if (extension == null) extension = JavaFileType.INSTANCE.getDefaultExtension();
    final String name = "myClass" + "." + extension;
    final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(name, JavaLanguage.INSTANCE, content, false, false);
    LanguageLevel highest = LanguageLevel.HIGHEST;
    LanguageLevel implicitClassesMinimumLevel = JavaFeature.IMPLICIT_CLASSES.getStandardLevel();
    if (highest.isLessThan(implicitClassesMinimumLevel)) {
      highest = implicitClassesMinimumLevel;
    }
    psiFile.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, highest);

    if (!(psiFile instanceof PsiJavaFile psiJavaFile)){
      throw new IncorrectOperationException("This template did not produce a Java class or an interface\n"+psiFile.getText());
    }
    final PsiClass[] classes = psiJavaFile.getClasses();
    if (classes.length == 0) {
      throw new IncorrectOperationException("This template did not produce a Java class or an interface\n"+psiFile.getText());
    }
    PsiClass createdClass = classes[0];
    String className;
    if (optionalClassName != null && createdClass instanceof PsiImplicitClass) {
      className = optionalClassName;
    }
    else {
      className = createdClass.getName();
    }
    JavaDirectoryServiceImpl.checkCreateClassOrInterface(directory, className);

    final LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);
    if (ll.compareTo(LanguageLevel.JDK_1_5) < 0) {
      if (createdClass.isAnnotationType()) {

View on GitHub (pinned to be881553f2)

Solutions

  1. Open Settings | Editor | File and Code Templates and check the offending template body renders valid Java (class/interface/enum declaration).
  2. Verify you are using the right template type: non-Java templates must not be created through the Java 'New Class' action.
  3. Use 'Go to definition'/preview on the template to inspect the expanded output; the exception message includes the exact text that failed to parse.

Example fix

// before (template body that yields no Java type)
{ "note": "${NAME}" }

// after
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
class ${NAME} {
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  PsiClass c = JavaCreateFromTemplateHandler.createFromTemplate(project, directory, name, template, props);
} catch (IncorrectOperationException e) {
  // message contains the expanded template text — inspect it to fix the template body
  showError(e.getMessage());
}

Prevention

When it happens

Trigger: Invoking 'New | Java Class' (or another Java file template) where the template body renders to non-Java content: a template edited to hold arbitrary text, a file-type mix-up so content is parsed as a different language, or a custom template named like a Java one but containing non-Java syntax.

Common situations: Users editing built-in class templates and accidentally breaking them; third-party plugins registering templates that route through JavaCreateFromTemplateHandler; template property expansion failing so only literal non-Java text remains.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/c65f8f912264a7ee. Report an issue: GitHub.