IntelliJ Error:java: java.lang.ExceptionInInitializerError

Viewed 21900

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)

6 Answers

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>*

I was facing same error when i tried to run my application in IntelliJ-2019.2 version. Below are the steps i followed to resolve this issue.

Versions:

IntelliJ : IDEA-IntelliJ-2019.2

Java : jdk1.8_221

  1. Go to below path in IntelliJ

File -> Project Structure -> Project -> Project SDK -> (select java version which you want to use )

(In my case under 'project SDK' java-11 was selected, I changed it to 'java8')

  1. Click on 'Apply' and then 'OK'.

enter image description here

enter image description here

I feel I ran into this issue because IntelliJ was trying to compile my java classes using in-built java-11 whereas my java classes are built on java-8. So when i explicitly configured java-8 in IntelliJ, It worked!! Hope this helps.

I started seeing this exception once I installed Java 11 in my machine. JAVA_HOME was by default pointing to Java 11 and my project was still in Java 8. Changing JAVA_HOME to Java 8 jdk fixed the issue for me.

If you have multiple projects each running on a different JDK, use this command to temporarily change the Java version per command.

JAVA_HOME=/path/to/JVM/jdk/Home mvn clean install

If you have recently updated your IDE then you can try these steps.

  1. Delete .idea directory for the idea project/workspace
  2. Then go to File -> Invalidate Caches / Restart...
  3. Once Idea is restarted re-add/import your module(s)

I faced a similar issue with JARs and Jena (while run from IntelliJ it works).
I was using Apache Jena v4.0.0 in my project and have built a JAR (with a main class for the JAR to act as a console app).
The JAR builts successfully with IntelliJ but when run throws java.lang.ExceptionInInitializerError ... Caused by: java.lang.NullPointerException. NPE suggests that something was not initialized properly.
The jar built with previous version Jena 3.17.0 works perfectly.

What I did to fix it
I've opened both the JARs, compared their META-INF folders and encountered the difference in

my.jar\META-INF\services\org.apache.jena.sys.JenaSubsystemLifecycle

The new version (jena v4.0.0) contains only one line:

org.apache.jena.tdb.sys.InitTDB

The old version (jena v3.17.0) contains two different lines:

org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ

I've added the old two lines to the file and repacked new JAR with it:

org.apache.jena.tdb.sys.InitTDB
org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ

It resolved my issue.
Update: recent Jena v4.4.0 builts with the same "bug".
I'm not an expert and there is probably a better way than patching a JAR by hand.
But I still hope that this solution will help someone like me.

Related