Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ Error:java: java.lang.ExceptionInInitializerError

Every time I encounter this exception in IntelliJ, I fix it trivially and forget the fix easily.

Code:

package whatever;
import org.junit.Test;
public class TestClass
{
    @Test
    void test() {}
}

Scenario:

  • Add new TestClass.
  • Right-click TestClass.
  • Select "Run 'TestClass'" to run test cases.

The "Messages Build" pane shows:

Information:javac 9-ea was used to compile java sources
Information:Module "dummy" was fully rebuilt due to project configuration/dependencies changes
Information:8/16/17 11:35 PM - Compilation completed with 1 error and 0 warnings in 1s 663ms
Error:java: java.lang.ExceptionInInitializerError

What can possibly go wrong?

What are the likely issues in this simple scenario?

IntelliJ: COMMUNITY 2017.1 (idea-IC-171.4424.56)

like image 975
uvsmtid Avatar asked Aug 16 '17 15:08

uvsmtid


People also ask

How do I fix Java Lang ExceptionInInitializerError?

We can resolve the java. lang. ExceptionInInitializerError by ensuring that static initializer block of classes does not throw any Runtime Exception. We can resolve also resolve this exception by ensuring that the initializing static variable of classes also doesn't throw any Runtime Exception.

What does Java Lang ExceptionInInitializerError mean?

An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable. As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism.

What is Java Lang ExceptionInInitializerError null?

The ExceptionInInitializerError indicates that an unexpected exception has occurred in a static initializer. Basically, when we see this exception, we should know that Java failed to evaluate a static initializer block or to instantiate a static variable.


2 Answers

To fix the issue, I do:

  • File -> Project Structure... -> Project Settings / Project -> Project SDK.
  • Change from "9-ea" to "1.8".

DETAILS

Apparently, the issue is discrepancies in selected JDK-s to build (java 9) and run (java 8).

I'm not sure how "9-ea" gets re-selected there for the same project - neither IntelliJ itself runs in "9-ea" JRE (according to Help -> About) nor JAVA_HOME env var is set to it nor other possible settings (like Maven -> Runner) suggest any "9-ea".

I also didn't manage to run the test under the same JDK (java 9) which it gets compiled under. However, it's unclear what JDK tests are run under because IntelliJ reports only about JDK for compilation.

like image 109
uvsmtid Avatar answered Oct 20 '22 22:10

uvsmtid


If you use Lombok: For me it was a solution to set the newest version for my maven lombok dependency in the pom.xml.

   *<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
        <version>1.18.8</version>
    </dependency>*
like image 33
SJX Avatar answered Oct 21 '22 00:10

SJX