我有一个我正在使用的开源java项目。当我直接使用源代码时,一切都正常,但是当我尝试从代码中创建一个jar并通过jar引用相同的类时,我得ExceptionInInitlaizerError
引用了Unknown Source
。稍后,它会显示NoClassDefFoundError
。下面是4次测试运行的堆栈跟踪:
用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
如果我直接将所有源包复制到项目中,并运行相同的代码,那么我得到正确的输出:
Test 1: true
Test 2: true
Test 3: true
Test 4: true
我创建了另一个测试jar,以确保我的jarring过程是正确的,并且我能够正确使用它。
为什么代码可以使用源而不是使用jar的任何想法?
EDT:根据要求制作jar的蚂蚁代码
请求我如何创建jar文件的代码。我使用示例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:使用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]
答案 0 :(得分:1)
查看帖子中的错误:
java.lang.NoClassDefFoundError: Could not initialize class mf.org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl
听起来你的程序使用了在创建可执行jar时不使用的外部jar。
你的jar没有看到它们,请确保你有类路径:
<classpathentry exported="true" kind="lib" path=" ...
^^^
<强> [编辑] 强>
ANT
部分之后property
中的添加:
<!-- a classpath for compile this project -->
<path id="build.classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
并在属性中添加:
<property name="lib.dir" value="libs"/>
这是如何将类路径添加到编译部分的示例:
<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>
希望它会对你有帮助,
答案 1 :(得分:1)
我与该项目的作者联系。他们使用eclipse导出jar文件。我注意到eclipse导出的jar文件和我的ant jar文件之间的jar大小差异。
我不确定我的蚂蚁脚本中缺少了什么(甚至尝试了**/*
,但没有取得胜利),但下面是详细信息,以防任何人遇到同样的问题。
File -> Export -> Java / JAR File -> Check "Export all output folders for checked projects"
下面是我用来让jar正常工作的设置的屏幕截图: