languagetool-org/languagetool · error · RuntimeException

Language was already instantiated, see the cause stacktrace

Error message

Language was already instantiated, see the cause stacktrace below.

What it means

SwissFrench's constructor is a singleton guard identical to the other French variants: the Language object must only be instantiated once through LT's language registry. On a second instantiation it throws RuntimeException, attaching the stack trace recorded at the first instantiation as the cause.

Source

Thrown at languagetool-language-modules/fr/src/main/java/org/languagetool/language/SwissFrench.java:32

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.language;

import java.util.Collections;
import java.util.List;

public class SwissFrench extends French {

  private static volatile Throwable instantiationTrace;

  public SwissFrench() {
    super(true);
    Throwable trace = instantiationTrace;
    if (trace != null) {
      throw new RuntimeException("Language was already instantiated, see the cause stacktrace below.", trace);
    }
    instantiationTrace = new Throwable();
  }

  @Override
  public String getName() {
    return "French (Switzerland)";
  }

  @Override
  public String[] getCountries() {
    return new String[] { "CH" };
  }

  @Override
  public List<String> getDefaultDisabledRulesForVariant() {
    List<String> rules = Collections.singletonList("DOUBLER_UNE_CLASSE");
    return rules;

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Use Languages.getLanguageForShortCode("fr-CH") instead of the constructor.
  2. Use the exception's cause trace to find the initial instantiation and remove the duplicate creation path.
  3. Ensure a single classloader loads the French language module.

Example fix

// before
Language lang = new SwissFrench();
// after
Language lang = Languages.getLanguageForShortCode("fr-CH");
Defensive patterns

Strategy: validation

Validate before calling

// Obtain the singleton instead of constructing:
Language lang = Languages.getLanguageForShortCode("fr-CH");

Try / catch

try {
  new SwissFrench();
} catch (RuntimeException e) {
  e.getCause().printStackTrace(); // shows first instantiation site
}

Prevention

When it happens

Trigger: Calling new SwissFrench() directly instead of Languages.getLanguageForShortCode("fr-CH")/getInstance; reflective instantiation by a framework; duplicate classloaders each constructing the language.

Common situations: Application code constructing language objects manually; serialization/deserialization creating new instances; test harnesses instantiating the language class.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/ac7ca80e76d46c02. Report an issue: GitHub.