user3829961
user3829961

Reputation: 23

What does an InvocationTargetException caused by a ExceptionInitializerError mean in java?

In the past couple of weeks, I've started to take up java again.

My recent java project involves taking business data from a website (phone number, address, company name, etc.), sorting through it, and inserting the data into an excel file on the user's desktop. I am not using this for anything (just to get back in the flow), but I might try something similar for financial market data. Anyways, the code itself works fine if I run it from the .jar file or through Eclipse.

However, when I try to create an executable file (through launch4j) and run it, it wasn't properly running as the jar file had. So I created another .exe file and changed it from GUI to console to debug the problem. I've listed the problem below.

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.ExceptionInInitializerError
        at java.base/javax.crypto.Cipher.getInstance(Cipher.java:548)
        at java.base/sun.security.ssl.SSLCipher.isTransformationAvailable(SSLCipher.java:510)
        at java.base/sun.security.ssl.SSLCipher.<init>(SSLCipher.java:499)
        at java.base/sun.security.ssl.SSLCipher.<clinit>(SSLCipher.java:82)
        at java.base/sun.security.ssl.CipherSuite.<clinit>(CipherSuite.java:69)
        at java.base/sun.security.ssl.SSLContextImpl.getApplicableSupportedCipherSuites(SSLContextImpl.java:343)
        at java.base/sun.security.ssl.SSLContextImpl$AbstractTLSContext.<clinit>(SSLContextImpl.java:556)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:340)
        at java.base/java.security.Provider$Service.getImplClass(Provider.java:1844)
        at java.base/java.security.Provider$Service.newInstance(Provider.java:1820)
        at java.base/sun.security.jca.GetInstance.getInstance(GetInstance.java:236)
        at java.base/sun.security.jca.GetInstance.getInstance(GetInstance.java:164)
        at java.base/javax.net.ssl.SSLContext.getInstance(SSLContext.java:184)
        at java.base/javax.net.ssl.SSLContext.getDefault(SSLContext.java:110)
        at java.base/javax.net.ssl.SSLSocketFactory.getDefault(SSLSocketFactory.java:83)
        at java.base/javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory(HttpsURLConnection.java:334)
        at java.base/javax.net.ssl.HttpsURLConnection.<init>(HttpsURLConnection.java:291)
        at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.<init>(HttpsURLConnectionImpl.java:81)
        at java.base/sun.net.www.protocol.https.Handler.openConnection(Handler.java:62)
        at java.base/sun.net.www.protocol.https.Handler.openConnection(Handler.java:57)
        at java.base/java.net.URL.openConnection(URL.java:1101)
        at org.jsoup.helper.HttpConnection$Response.createConnection(HttpConnection.java:892)
        at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:729)
        at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:707)
        at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:297)
        at org.jsoup.helper.HttpConnection.get(HttpConnection.java:286)
        at SpiderTest.main(SpiderTest.java:64)

Looking through my code, it seems that the error occurs at Line 64, which is:

Document document = Jsoup.connect(url).get();

I've tried all other solutions on StackOverflow and other sites, and none of them seem to fix this issue. The main jar files I've used are Jsoup (for web) and Apache (for excel). I exported the excel project as a runnable jar file and packaged all required libraries into the generated jar. I'm at a loss on where to go, or if I'm an idiot.

Upvotes: 2

Views: 2116

Answers (1)

Y2020-09
Y2020-09

Reputation: 1

I would prefer to just post this as a comment, but there is just a little too much text. The Java exception ExceptionInInitializerError has been thrown (in my experience) whenever a static initializer is throwing an exception (for whatever reason) when the Class Loader attempts to load a particular class. If this doesn't make sense, yet, look up the term Static Initializer on the Sun Documentation Websites, or look up it up on Stack Overflow or Google. A Static Initializer is a part of a Java Class that is called whenever a class is first loaded into memory - it is surrounded by the { and } squiggly braces, but doesn't have a method name.

I've written (Quite a bit) of Java, but I don't know if a constructor could cause this, since most of my classes are static classes.

Now, to summarize so far, all that the "Caused By" says is that the class loader tried to load a class named Cipher using the method javax.crypto.Cipher.getInstance(Cipher.java:548) and it couldn't load that class.

When this exception threw, you got a InvokationTargetException - which I am also very familiar with and that one is much easier to explain... Because the Class Loader could not load the class mentioned by the previous exception, when the line of code that you have called tried to get an instance of that class, it was unable to - obviously because it could not even load the class from the JAR File in the first place.

Last point, if you are running code from some class file - wherever you are running it from - and it is unable to load the JSoup JAR File, perhaps this is causing it. I mean, if you are building executables, you need to figure out whether or not this executable can see the JSoup JAR File in the CLASSPATH Environment Variable.

Thoughts:

  • First, if you ever put an addendum (in your question) explaining what you mean by executable I would be less confused because I have not made one before. I have only made executable .jar files
  • Does your JAR File contain the JSoup Classes?
  • Did you set the CLASSPATH when you run this executable?
  • Are you using the right JDK - one that contains this class Cipher - which is clearly needed by the JSoup JAR? (Since it is failing to load javax.crypto.Cipher.getInstance())... Not all JDK's have access to class javax.crypto.Cipher
  • Sometimes multiple JDK's can be installed on them, and not all of them have access to the same libraries.

There could be other reasons...

Upvotes: 2

Related Questions