一个由ExceptionInitializerError引起的InvocationTargetException在Java中是什么意思?

huangapple go评论68阅读模式
英文:

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

问题

过去几周里,我开始重新学习Java编程。

我最近的Java项目涉及从网站上获取业务数据(电话号码、地址、公司名称等),对数据进行排序,然后将数据插入用户桌面上的Excel文件中。我并没有将这用于任何实际目的(只是为了重新进入状态),但我可能会尝试类似的方法来处理金融市场数据。总之,如果我从.jar文件或通过Eclipse运行代码本身是没有问题的。

然而,当我尝试创建一个可执行文件(通过launch4j)并运行它时,它并没有像jar文件那样正常运行。所以我创建了另一个.exe文件,并将其从GUI更改为控制台以调试问题。我在下面列出了问题。

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)

浏览了我的代码后,似乎错误发生在第64行,即:

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

我已经尝试了StackOverflow和其他网站上的所有其他解决方案,但似乎没有一个能够解决这个问题。我主要使用的jar文件是Jsoup(用于网络)和Apache(用于Excel)。我将Excel项目导出为可运行的jar文件,并将所有必需的库文件打包到生成的jar文件中。我不知道该怎么办,或者是否是我的问题。

英文:

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 &quot;main&quot; 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.&lt;init&gt;(SSLCipher.java:499)
        at java.base/sun.security.ssl.SSLCipher.&lt;clinit&gt;(SSLCipher.java:82)
        at java.base/sun.security.ssl.CipherSuite.&lt;clinit&gt;(CipherSuite.java:69)
        at java.base/sun.security.ssl.SSLContextImpl.getApplicableSupportedCipherSuites(SSLContextImpl.java:343)
        at java.base/sun.security.ssl.SSLContextImpl$AbstractTLSContext.&lt;clinit&gt;(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.&lt;init&gt;(HttpsURLConnection.java:291)
        at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.&lt;init&gt;(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.

答案1

得分: 2

我更倾向于将这段内容作为评论发表,但文字略多。在我的经验中,当“类加载器”尝试加载特定类时,如果一个“静态初始化程序”由于某种原因抛出异常,通常会抛出Java异常ExceptionInInitializerError。如果现在还不太明白,可以查阅Sun文档网站上的术语“静态初始化程序”,或者在Stack Overflow或Google上进行查找。一个“静态初始化程序”是Java类的一部分,在类首次加载到内存时调用 - 它被大括号{}包围,但没有方法名称。

我写过(相当多的)Java代码,但我不知道构造函数是否会导致这种情况,因为我的大多数类都是“静态类”。

现在,总结一下到目前为止,“Caused By”的全部内容就是类加载器尝试使用方法javax.crypto.Cipher.getInstance(Cipher.java:548)加载名为Cipher的类,但无法加载该类。

当抛出此异常时,您会得到一个InvocationTargetException - 我对这个异常也非常熟悉,这个异常容易解释... 因为“类加载器”无法加载前一个异常中提到的类,所以在您调用的代码行尝试获取该类的实例时,它无法实现 - 很明显,因为它甚至无法从“JAR文件”中首次加载类。

最后一点,如果您正在从某个类文件运行代码 - 无论您从何处运行代码 - 而且无法加载“JSoup JAR文件”,那么可能是这个问题的原因。我的意思是,如果您正在构建可执行文件,您需要弄清楚这个可执行文件是否能在“CLASSPATH”环境变量中看到“JSoup JAR文件”。

想法:

  • 首先,如果您在问题中加入附注,解释一下您所说的“可执行文件”,我会少一些困惑,因为我以前没有制作过。我只制作过“可执行的.jar文件”。
  • 您的“JAR文件”是否包含“JSoup”类?
  • 在运行此可执行文件时,是否设置了“CLASSPATH”?
  • 您是否在使用正确的JDK - 一个包含此“Cipher类”的JDK,这显然是JSoup JAR所需的?(因为无法加载javax.crypto.Cipher.getInstance())... 并非所有的JDK都可以访问class javax.crypto.Cipher
  • 有时计算机上可能安装了多个JDK,并且并非所有都可以访问相同的库。

还可能有其他原因...

英文:

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

huangapple
  • 本文由 发表于 2020年10月16日 03:56:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64378829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定