JetBrains/intellij-community · error · CantRunException

No main class defined

Error message

No main class defined

What it means

ApplicationCommandLineState.createJavaParameters runs a non-blocking read action that configures the classpath; inside it, if mainClass is null it throws CantRunException('No main class defined'). This is the standard guard for a Java Application run configuration whose main class field is empty or unresolved.

Source

Thrown at java/execution/impl/src/com/intellij/execution/application/ApplicationCommandLineState.java:67

      .expireWith(expireReadsWith)
      .executeSynchronously();

    params.setMainClass(mainClass);
    try {
      JavaParametersUtil.configureConfiguration(params, configuration);
    }
    catch (ProgramParametersConfigurator.ParametersConfiguratorException e) {
      throw new ExecutionException(e);
    }

    final JavaRunConfigurationModule module = configuration.getConfigurationModule();
    try {
      ReadAction.nonBlocking(() -> {
        final String jreHome = getTargetEnvironmentRequest() == null && configuration.isAlternativeJrePathEnabled() ? configuration.getAlternativeJrePath() : null;
        if (module.getModule() != null) {
          DumbService.getInstance(module.getProject()).runWithAlternativeResolveEnabled(() -> {
            if (mainClass == null) {
              throw new CantRunException(ExecutionBundle.message("no.main.class.defined.error.message"));
            }
            int classPathType = JavaParametersUtil.getClasspathType(module, mainClass, false,
                                                                    isProvidedScopeIncluded());
            // todo effects must be applied in non-cancellable section
            JavaParametersUtil.configureModule(module, params, classPathType, jreHome);
          });
        }
        else {
          // todo effects must be applied in non-cancellable section
          JavaParametersUtil.configureProject(module.getProject(), params, JavaParameters.JDK_AND_CLASSES_AND_TESTS, jreHome);
        }
        return null;
      })
        .expireWith(expireReadsWith)
        .executeSynchronously();
    }
    catch (Exception e) {
      ExecutionException executionException = ExceptionUtil.findCause(e, ExecutionException.class);

View on GitHub (pinned to be881553f2)

Solutions

  1. Edit the run configuration and set 'Main class' to a class containing 'public static void main(String[])'
  2. If the class exists but is not detected, make sure the file is a valid class with a proper main signature and the module is compiled
  3. For checked-in configurations, update the main class FQN after refactoring/moving the class
  4. If the main class uses preview features or is in a non-compiling module, fix compilation first so the class resolves

Example fix

// before: RunConfiguration mainClass = null
// after: runConfig.setMainClassName("com.example.MyApp");
Defensive patterns

Strategy: validation

Validate before calling

// before starting, ensure the main class is set and resolvable
String main = configuration.getMainClassName();
if (main == null || main.isBlank() || JavaPsiFacade.getInstance(project).findClass(main, GlobalSearchScope.allScope(project)) == null) {
  return error("Main class is missing or does not resolve");
}

Try / catch

try {
  super.createJavaParameters();
} catch (CantRunException e) {
  // surface 'No main class defined' to the user with a link to edit the configuration
  showEditConfigurationDialog(e);
}

Prevention

When it happens

Trigger: Application run configuration with an empty 'Main class' field, or a main class name that resolved to null during state creation (e.g. configuration created programmatically or from a launcher template without setting the main class).

Common situations: Newly created 'Application' configuration where the user never picked a main class; shared/checked-in .run XML referencing a class that no longer exists; templates in plugins creating configurations without a main class; running a scratch/main file before it has a valid main method.

Related errors


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