JetBrains/intellij-community · error · IncorrectOperationException
Cannot create class-file
Error message
Cannot create class-file
What it means
Thrown by PsiJavaDirectoryImpl.checkCreateFile() when a file whose type resolves to JavaClassFileType (.class) is about to be created inside a directory that the project file index reports as source (isInSource). The platform forbids writing compiled .class files into source roots via PSI directory APIs. It is an IncorrectOperationException raised before the file is created.
Source
Thrown at java/java-impl/src/com/intellij/psi/impl/file/PsiJavaDirectoryImpl.java:45
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.impl.PsiManagerEx;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public class PsiJavaDirectoryImpl extends PsiDirectoryImpl {
private static final Logger LOG = Logger.getInstance(PsiJavaDirectoryImpl.class);
PsiJavaDirectoryImpl(@NotNull PsiManagerEx manager, @NotNull VirtualFile file) {
super(manager, file);
}
@Override
public void checkCreateFile(@NotNull String name) throws IncorrectOperationException {
FileType type = FileTypeManager.getInstance().getFileTypeByFileName(name);
if (type == JavaClassFileType.INSTANCE && ProjectRootManager.getInstance(getProject()).getFileIndex().isInSource(getVirtualFile())) {
throw new IncorrectOperationException("Cannot create class-file");
}
super.checkCreateFile(name);
}
@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
if (element instanceof PsiClass) {
String name = ((PsiClass)element).getName();
if (name != null) {
PsiClass newClass = JavaDirectoryService.getInstance().createClass(this, name);
return newClass.replace(element);
}
else {
LOG.error("not implemented");
return null;
}
}View on GitHub (pinned to be881553f2)
Solutions
- Write .class files to the module's build output directory or a plain (non-source) directory instead of a source root.
- Fix the generator/build configuration so compiled artifacts go to the compiler output path, not src/.
- If the directory should hold .class files (e.g. resource bundles), unmark it as source root in Project Structure > Modules.
Example fix
// before
VirtualFile srcDir = ...; // source root
dir.createFile("Foo.class"); // throws
// after
VirtualFile outDir = CompilerPaths.getModuleOutputDirectory(module, false);
outDir.createChildData(this, "Foo.class"); Defensive patterns
Strategy: validation
Validate before calling
FileType type = FileTypeManager.getInstance().getFileTypeByFileName(fileName);
boolean isSource = ProjectRootManager.getInstance(project).getFileIndex().isInSource(dir.getVirtualFile());
if (type == JavaClassFileType.INSTANCE && isSource) {
// redirect output to build directory instead of dir.createFile(fileName)
} Try / catch
try { dir.checkCreateFile("Foo.class"); } catch (IncorrectOperationException e) { /* .class into source root forbidden; choose output dir */ } Prevention
- Never write compiled artifacts into source roots; target compiler output directories.
- In generators, derive the destination from CompilerPaths/module output, not from the source PsiDirectory.
- Mark directories meant for raw resources as excluded or resource roots, not sources.
When it happens
Trigger: Calling PsiDirectory.checkCreateFile("Foo.class") or createFile with a .class name where the directory is under a marked source root; plugin code that copies build output back into src/; templates or generators emitting .class artifacts into source directories.
Common situations: A plugin copies compiled classes next to sources; a user drags a .class file into a source folder through an IDE action backed by checkCreateFile; build scripts or code generators misconfigured to output into src/main/java instead of build output.
Related errors
- Cannot create {0} - incorrect {1} template.
- Cannot create class in invalid package: '{qualifiedName}'
- Cannot bind to {element}
- error.package.set.position.parsing.error
- Cannot create package '{0}' in source folder {1}
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/5060c54813300f43.
Report an issue: GitHub.