JetBrains/intellij-community · error · RuntimeConfigurationError

No main class specified

Error message

No main class specified

What it means

JavaScratchConfiguration.checkConfiguration validates a Java scratch run configuration before execution. If the main class name derived from the scratch file is null or empty, it throws RuntimeConfigurationError with 'No main class specified', aborting the run at the configuration-check stage.

Source

Thrown at java/execution/impl/src/com/intellij/execution/scratch/JavaScratchConfiguration.java:66

/**
 * @author Eugene Zhuravlev
 */
public final class JavaScratchConfiguration extends ApplicationConfiguration {
  JavaScratchConfiguration(String name, @NotNull Project project, @NotNull ConfigurationFactory factory) {
    super(name, project, factory);
  }

  @Override
  public boolean isBuildProjectOnEmptyModuleList() {
    return false;
  }

  @Override
  public void checkConfiguration() throws RuntimeConfigurationException {
    JavaParametersUtil.checkAlternativeJRE(this);
    final String className = getMainClassName();
    if (className == null || className.isEmpty()) {
      throw new RuntimeConfigurationError(ExecutionBundle.message("no.main.class.specified.error.text"));
    }
    if (getScratchFileUrl() == null) {
      throw new RuntimeConfigurationError(JavaCompilerBundle.message("error.no.scratch.file.associated.with.configuration"));
    }
    if (getScratchVirtualFile() == null) {
      throw new RuntimeConfigurationError(JavaCompilerBundle.message("error.associated.scratch.file.not.found"));
    }
    ProgramParametersUtil.checkWorkingDirectoryExist(this, getProject(), getConfigurationModule().getModule());
    JavaRunConfigurationExtensionManager.checkConfigurationIsValid(this);
  }

  @Override
  public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment env) throws ExecutionException {
    final JavaCommandLineState state = new JavaApplicationCommandLineState<>(this, env) {
      @Override
      protected JavaParameters createJavaParameters() throws ExecutionException {
        final JavaParameters params = super.createJavaParameters();
        // After params are fully configured, additionally ensure JAVA_ENABLE_PREVIEW_PROPERTY is set,

View on GitHub (pinned to be881553f2)

Solutions

  1. Add a valid main method to the scratch file: public static void main(String[] args) { ... }
  2. Make sure the class name in the scratch matches a top-level class (script-style scratch files without a class may also fail depending on version)
  3. Re-run after saving the file so the main class is re-detected

Example fix

// before (scratch)
class Demo { static void run() {} }
// after
class Demo { public static void main(String[] args) { run(); } }
Defensive patterns

Strategy: validation

Validate before calling

// before running a scratch, check it declares a main method
String main = JavaScratchConfigurationHelper.getMainClassName(scratchFile);
if (main == null || main.isEmpty()) {
  return notify("Scratch must contain 'public static void main(String[])'");
}

Try / catch

try {
  configuration.checkConfiguration();
} catch (RuntimeConfigurationError e) {
  // no main class: open the scratch and let the user add main()
}

Prevention

When it happens

Trigger: Running a Java scratch file (or a JavaScratchConfiguration created via 'Run' in a scratch) where the scratch file contains no main method declaration the parser can find, so getMainClassName() returns null/empty.

Common situations: User writes a scratch class without 'public static void main' and hits Run; scratch file has a main-like method with wrong signature (e.g. no String[] arg); the scratch file's language level makes detection fail.

Related errors


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