Xerces-for-Android NoClassDefFoundError When Using

2019-07-27 05:19发布

问题:

I'm working with Xerces-for-Android: https://code.google.com/p/xerces-for-android/

If I copy the source code into a simple test project (java project, not android project), I can run my xml validator tests without any issues.

When I make a jar from the source files then use that in my class path instead of the source, I get NoClassDefFoundError despite the files being present in the jar.

Below is the error message:

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

Below is my ant script to make the jar:

<project name="XercesForAndroid" 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>

The class files exist in the jar. Do I need to do anything special in the manifest, or am I missing something obvious?

回答1:

In an android project you have to add your jar file to the libs/ folder, and ADT will add it automaticly to the classpath. It wont find the jar at runtime if you add it the old way in eclipse with the build path/add jar.

Just take your jar files and put them in the libs/ folder and ADT will take care of the rest



回答2:

I got in contact with the authors of the project. They used eclipse to export the jar file. I noticed a size difference in the jar size between the eclipse exported jar file and my ant jar file.

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:



标签: java android ant