uvsmtid
uvsmtid

Reputation: 4295

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:

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)

Upvotes: 8

Views: 42908

Answers (6)

Samantra
Samantra

Reputation: 167

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.

Upvotes: 0

Tapan Hegde
Tapan Hegde

Reputation: 1328

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.

Upvotes: 3

Michael Scott
Michael Scott

Reputation: 580

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

Upvotes: 1

SJX
SJX

Reputation: 1239

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

Upvotes: 8

Pravin Jadhav
Pravin Jadhav

Reputation: 151

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)

Upvotes: 0

uvsmtid
uvsmtid

Reputation: 4295

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.

Upvotes: 30

Related Questions