0

私が取り組んでいるオープンソースのJavaプロジェクトがあります。ソースコードを直接操作すると、すべてが適切に機能しますが、コードから jar を作成し、jar を介して同じクラスを参照しようとするとExceptionInInitlaizerErrorUnknown 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 を作成して、jar プロセスが正しく、適切に使用できることを確認しました。

コードがソースでは機能するが、jar では機能しない理由はありますか?

EDT: 要求に応じて、jar を作成するための ant コード

jar ファイルの作成方法に関するコードを要求しました。jar を作成するために jar のサンプル ant コードを使用しました。以下は私の 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]
4

2 に答える 2

1

私はプロジェクトの作者と連絡を取った。彼らはEclipseを使用してjarファイルをエクスポートしました。Eclipse でエクスポートされた jar ファイルと私の ant jar ファイルの jar サイズにサイズの違いがあることに気付きました。

私のantスクリプトで何が欠けていたのかよくわかりませんが(試してみ**/*ましたが、勝てませんでした)、誰かが同じ問題を抱えている場合の詳細を以下に示します。

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

以下は、jar を適切に動作させるために使用した設定のスクリーンショットです。

ここに画像の説明を入力

于 2013-09-19T15:35:34.017 に答える
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=" ...
                           ^^^

[編集]

セクションのANTpropertyに次を追加します。

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

それがあなたを助けることを願って、

于 2013-09-18T22:28:23.930 に答える