java - Xerces-for-Android 使用 Jar 而不是源代码时出现 NoClassDefFoundError

标签 java android ant

我正在使用 Xerces-for-Android:https://code.google.com/p/xerces-for-android/

如果我将源代码复制到一个简单的测试项目(java 项目,而不是 android 项目),我可以毫无问题地运行我的 xml validator 测试。

当我从源文件创建一个 jar 然后在我的类路径而不是源中使用它时,尽管文件存在于 jar 中,但我得到了 NoClassDefFoundError

错误信息如下:

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

下面是我制作 jar 的 ant 脚本:

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

类文件存在于 jar 中。我是否需要在 list 中做任何特别的事情,或者我是否遗漏了一些明显的事情?

最佳答案

在 android 项目中,您必须将 jar 文件添加到 libs/文件夹中,ADT 会自动将其添加到类路径中。如果您在 Eclipse 中使用构建路径/添加 jar 以旧方式添加它,它不会在运行时找到 jar。

只需将您的 jar 文件放入 libs/文件夹中,ADT 将处理其余部分

关于java - Xerces-for-Android 使用 Jar 而不是源代码时出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18881431/

相关文章:

java - 收到此错误 : "javax.net.ssl.SSLHandshakeException: no cipher suites in common"

java - 使用 Spring Boot 从 LDAP 排序记录

java - Android:Java 是否对许多类效率较低

Android putExtra 停止工作

两点之间的Android距离

Java 8 Javascript 引擎向后兼容

java - 如何在 Delphi 中调试从 Java 调用的 DLL?

Java List<String> 到 Map<String, Integer> 的转换

eclipse - 为什么 CDT 在 Ant 上重建我的 C 项目并在不相关的项目中构建?

java - Ant 构建不工作 : unable to find a javac compiler