简体   繁体   English

jar ExceptionInInitializerError

[英]jar ExceptionInInitializerError

I have an open source java project that I'm working with. 我有一个我正在使用的开源java项目。 When I work with the source code directly, everything works properly, however when I try to make a jar out of the code and reference the same classes through the jar I get ExceptionInInitlaizerError with references of Unknown Source . 当我直接使用源代码时,一切都正常,但是当我尝试从代码中创建一个jar并通过jar引用相同的类时,我得到ExceptionInInitlaizerError ,引用了Unknown Source Later on, it shows NoClassDefFoundError . 稍后,它显示NoClassDefFoundError Below is the stack trace for 4 test runs: 以下是4次测试运行的堆栈跟踪:

Output with jar: 用jar输出:

java.lang.ExceptionInInitializerError
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:8)
Caused by: java.lang.RuntimeException: internal error
    at mf.org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl.applyFacets1(Unknown Source)
    at mf.org.apache.xerces.impl.dv.xs.BaseSchemaDVFactory.createBuiltInTypes(Unknown Source)
    at mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl.createBuiltInTypes(Unknown Source)
    at mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl.<clinit>(Unknown Source)
    ... 9 more
Test 1: false
java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:11)
Test 2: false
java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:14)
Test 3: false
java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:17)
Test 4: false

If I copy all of the source packages into the project directly, and run the same code then I get the proper output: 如果我直接将所有源包复制到项目中,并运行相同的代码,那么我得到正确的输出:

Test 1: true
Test 2: true
Test 3: true
Test 4: true

I created another test jar, to make sure that my jarring process was correct and I was able to use it properly. 我创建了另一个测试jar,以确保我的jarring过程是正确的,我能够正确使用它。

Any ideas why the code will work with the source but not with the jar? 任何想法为什么代码将使用源而不是jar?

EDT: The ant code to make the jar, as requested EDT:根据要求制作jar的蚂蚁代码

Requested code on how I create the jar file. 请求如何创建jar文件的代码。 I used the example ant code for jarring to make the jar, the following is my ant file: 我使用示例ant作为jarring来制作jar,以下是我的ant文件:

<project name="Xerces-For-Android" default="dist" basedir=".">
    <description>
        Builds jar for Xerces-For-Android
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="clean, init" description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac verbose="true" target="1.6" srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the .jar file -->
    <jar jarfile="${dist}/lib/Xerces-For-Android.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

EDT: Added Verbose to the run of the program with jar file, below is a portion of the output EDT:使用jar文件为程序运行添加了详细信息,下面是输出的一部分

[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
...
[Loaded java.lang.Enum from shared objects file]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$UnionToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$ParenToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$ClosureToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$ConcatToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$ConditionToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$ModifierToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$CharToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.Token$StringToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]
[Loaded mf.org.apache.xerces.impl.xpath.regex.RangeToken from file:/C:/path/to/workspace/Sandbox/libs/Xerces-For-Android.jar]

[Loaded java.util.ResourceBundle from shared objects file]
[Loaded java.util.ResourceBundle$1 from shared objects file]
[Loaded java.util.ResourceBundle$Control from shared objects file]
[Loaded java.util.Arrays$ArrayList from shared objects file]
[Loaded java.util.Collections$UnmodifiableCollection from shared objects file]
[Loaded java.util.Collections$UnmodifiableList from shared objects file]
[Loaded java.util.Collections$UnmodifiableRandomAccessList from shared objects file]
[Loaded java.util.ResourceBundle$CacheKey from shared objects file]
[Loaded java.util.ResourceBundle$CacheKeyReference from shared objects file]
[Loaded java.util.ResourceBundle$LoaderReference from shared objects file]
[Loaded java.util.ResourceBundle$Control$1 from shared objects file]
[Loaded sun.misc.Launcher$1 from shared objects file]
[Loaded sun.misc.Launcher$2 from shared objects file]
[Loaded java.net.URLClassLoader$2 from shared objects file]
[Loaded java.util.ResourceBundle$BundleReference from shared objects file]
[Loaded java.lang.ExceptionInInitializerError from shared objects file]
java.lang.ExceptionInInitializerError
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:8)
Caused by: java.lang.RuntimeException: internal error
    at mf.org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl.applyFacets1(Unknown Source)
    at mf.org.apache.xerces.impl.dv.xs.BaseSchemaDVFactory.createBuiltInTypes(Unknown Source)
    at mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl.createBuiltInTypes(Unknown Source)
    at mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl.<clinit>(Unknown Source)
    ... 9 more
Test 1: false
java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:11)
Test 2: false
java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:14)
Test 3: false
java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.dv.SchemaDVFactory.getInstance(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at mf.javax.xml.validation.SchemaFactory.newSchema(Unknown Source)
    at xml.MFXMLUtil.validate(MFXMLUtil.java:33)
    at xml.MFXMLTester.main(MFXMLTester.java:17)
Test 4: false
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]

See error in your post: 查看帖子中的错误:

java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl

Sounds like your program uses external jar that you don't used when created executable jar. 听起来你的程序使用的是创建可执行jar时不使用的外部jar。

Your jar doesn't see them, make sure you have in classpath: 你的jar没有看到它们,请确保你在classpath中:

<classpathentry exported="true" kind="lib" path=" ...
                           ^^^

[EDIT] [编辑]

in ANT after property section add: ANT property部分后添加:

    <!-- a classpath for compile this project -->
<path id="build.classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

and in properties add: 并在属性中添加:

<property name="lib.dir"          value="libs"/>

and this is example how to add classpath to compilation section: 这是如何将类路径添加到编译部分的示例:

<target name="compile"  depends="copy" description="Compiles java source code" >
    <echo>Start Compiling Code</echo>
    <javac srcdir="${source.dir}" destdir="${build.dir}/${classes.dir}" classpathref="build.classpath" includeantruntime="false"/>
    <echo>End Compiling Code</echo>
</target>

Hope it will help you, 希望它能帮到你,

I got in contact with the authors of the project. 我与该项目的作者联系。 They used eclipse to export the jar file. 他们使用eclipse导出jar文件。 I noticed a size difference in the jar size between the eclipse exported jar file and my ant jar file. 我注意到eclipse导出的jar文件和我的ant jar文件之间的jar大小差异。

Not really sure what I was missing in my ant script (even tried **/* , but to no prevail), however below are the details in case anyone has the same issue. 我不确定我的蚂蚁脚本中缺少了什么(甚至尝试了**/* ,但没有获胜),但下面是详细信息以防任何人有同样的问题。

File -> Export -> Java / JAR File -> Check "Export all output folders for checked projects"

Below is a screenshot of the settings I used to get the jar to work properly: 下面是我用来使jar正常工作的设置的屏幕截图:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM